Jump to content

[SOLVED] please help with regex...


tomasd

Recommended Posts

Somebody helped me to extract price from string, but I've realised that is not enough. Could somebody please tell me how to extract other information? I have

<?php

        $data = <<<DATA
        Regular FareAdult34.99 GBPMon, 15 Sep 08FlightFR 21112:00 Depart13:15 Arrive
DATA;

        function regex_price($data) {
                preg_match_all("/Regular FareAdult([\d.]+)/", $data, $result_price, PREG_PATTERN_ORDER);
                return $result_price;
        }
        print_r(regex_price($data));
?>

How do I join other regex'ions to extract FR211 from "FlightFR211", 12:00 from "12:00 Depart" and 13:15 from "13:15 Arrive"?

To extract time I'm using;

/(\d{2}:\d{2}) Depart/

/(\d{2}:\d{2}) Arrive/

Thanks very much for your help in advance!

Link to comment
Share on other sites

I'm trying this;

<?php

        $data = <<<DATA
        Regular FareAdult34.99 GBPMon, 15 Sep 08FlightFR 21112:00 Depart13:15 Arrive
DATA;

        function regex_price($data) {

                $regex_fare = "Regular FareAdult([\d.]+)";
                $regex_depart = "(\d{2}:\d{2}) Depart";
                $regex_arrive = "(\d{2}:\d{2}) Arrive";

                preg_match_all("/$regex_fare|$regex_depart|$regex_arrive/", $data, $result_price, PREG_PATTERN_ORDER);
                return $result_price;
        }
        print_r(regex_price($data));
?>

And result I'm getting is;

Array
(
    [0] => Array
        (
            [0] => Regular FareAdult34.99
            [1] => 12:00 Depart
            [2] => 13:15 Arrive
        )

    [1] => Array
        (
            [0] => 34.99
            [1] =>
            [2] =>
        )

    [2] => Array
        (
            [0] =>
            [1] => 12:00
            [2] =>
        )

    [3] => Array
        (
            [0] =>
            [1] =>
            [2] => 13:15
        )

)

Is there any way of making above like this;

Array
(
    [0] => Array
        (
            [0] => Regular FareAdult34.99
            [1] => 12:00 Depart
            [2] => 13:15 Arrive
        )

    [1] => Array
        (
            [0] => 34.99
            [1] => 12:00
            [2] => 13:15
        )
)

Link to comment
Share on other sites

<pre>
<?php

        $data = <<<DATA
        Regular FareAdult34.99 GBPMon, 15 Sep 08FlightFR 21112:00 Depart13:15 Arrive
DATA;

        function regex_price($data) {

		preg_match_all('/
			(Regular\sFareAdult)?
			(\d{1,2}[:.]\d{1,2})
			(?(1)|\s(?:Depart|Arrive))
		/x', $data, $result_price, PREG_PATTERN_ORDER);
		array_splice($result_price, 1, 1);
		return $result_price;
        }

        print_r(regex_price($data));
?>
</pre>

Link to comment
Share on other sites

wow it does exactly what it says on a tin!!! Thanks very much, I'm not very good with regex, in fact I have no clue about regex...

The syntax you came up with makes me even more confused. At your leisure could you please tell me if your syntax could be made more simpler, I don't mind it being long and repetitive as long as you could clearly define which part of regex takes which part from $data

i.e.

<?php
$regex_fare = "Regular FareAdult([\d.]+)";
$regex_flight = "FlightFR (\d{3})";
$regex_depart = "(\d{2}:\d{2}) Depart";
$regex_arrive = "(\d{2}:\d{2}) Arrive";

preg_match_all("/$regex_fare|$regex_flight|$regex_depart|$regex_arrive/", $data, $result, PREG_PATTERN_ORDER);
?>

 

Link to comment
Share on other sites

That's the only way to get the array output you want without extra non-regex programming. The expression itself is rather simple, actually:

 

(Regular\sFareAdult)? Match this string 0 to 1 times, i..e, it's optional

(\d{1,2}[:.]\d{1,2}) Match 1 to 2 digits, a colon or period, and 1 to 2 digits

(?(1)|\s(?:Depart|Arrive)) If the first match succeeded, we're done; otherwise, look for whitespace followed by "Depart" or "Arrive"

 

See more on conditionals here.

Link to comment
Share on other sites

I've figured I just keep all as is and use ;

                // remove empty values from $result[$i]
                $result_size = count($result);
                for ($i = 0; $i < $result_size; $i++){
                  foreach($result[$i] as $key => $value) {
                    if($value == "") {
                      unset($result[$i][$key]);
                        }
                      }
                    // reindex array[$i]
                    $result[$i] = array_values($result[$i]);
                   }
                // SPECIAL OFFERAdult array is $result[1]
                $price_array = $result[1];
                // FlightFR array is $result[2]
                $flight_array = $result[2];
                // Depart array is $result[3]
                $depart_array = $result[3];
                // Arrive array is $result[4]
                $arrive_array = $result[4];

then I combine all arrays with

                /* create multi dimentional $combined_array */
                for ($i = 0; $i < $price_array_length; $i++) {
                    $combined_array[] = array(
                          'type' => "1",
                          'price' => $price_array[$i],
                          'flight' => $flight_array[$i],
                          'depart' => $depart_array[$i],
                          'arrive' => $arrive_array[$i]
                          );
                      }
                return $combined_array;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.