Question

So I'm parsing information from another website using Guzzle and Symfony 2 DOMCrawler. And there is a string I parse and need to explode. The thing is, that everytime I it's different. So there is no way I can create constant patern, since their length is different.

For example, first time I get DY947CPHVNO and another time I get DA9128VNOBGODY989BGOCPH. My goal is to explode it like that:

I'll explain these strings, they are not so complicated. I'm parsing them from flight reservation website. So first 4-6 symbols are flight number then other ones are departures and arivals (their lengths are 3 symbols).

//first string DY947CPHVNO - it means that it's flight number DY947 from CPH to VNO
//DY947 CPH VNO
$string1 = DY947; //flight number
$string2 = CPH; //from CPH
$string3 = VNO; //to VNO

//first string DA9128VNOBGODY989BGOCPH - it means that it's flight number DA9128 from VNO to BGO then flight number DY989 from BGO to CPH 
//DA9128 VNO BGO DY989 BGO CPH
$string1 = DA9128; //flight number
$string2 = VNO; //from VNO
$string3 = BGO; //to BGO
$string4 = DY989; //flight number
$string5 = BGO; // from BGO
$string6 = CPH; //to CPH

Second string means that to reach destination person needs 2 flights.

The only thing that is dinamic here, is flight number, I can't use patern with substr and regex since I don't know it's length. Cities always contain 3 symbols. and it's not changing.

Any help? I saw similar questions here in stackoverflow, but didn't find answered right.

Was it helpful?

Solution

First of all please try to learn how about regex. Here is the great site. If you do not learn yourself, then you have to beg for help to others like this one every time. It is always better to provide your example that you have done so far, so that the SO community can continue from there to guide you.

Here is a hedious regex:

preg_match("/^([a-z0-9]{4,6})([a-z0-9]{3})([a-z0-9]{3})(?:([a-z0-9]{4,5})(\\3)([a-z0-9]{3}))?$/i", $numbers, $m);
print_r($m);

Use this site, and paste the regex over there to get explanation of it.

OTHER TIPS

Try using this:

(?:[A-Z]{2}\d+[A-Z]{6})+

Two uppercase letters followed by any number of digits (flight number) followed by 6 uppercase letters (from/to); one or more occurrences of this group (for stop overs).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top