Hello,

I have the following string and would like to use preg_match() to extract parts of the string.

Yang club
4931 Wyaconda RdRockville, IA  20852
301-937-3401
Monday - Friday - 3:00 - 10:00PM and Saturday & Sunday -12:00 - 8:00PM

another possible string is:

Kahler Hall 5440 Old Tucker Row
Columbia, MD  21044
Wednesday - Friday - 6:00 - 10:00PM and Saturday & Sunday 10:00 - 8:00PM

I would like to use preg_match() to get everything to the right or after the zipcode (formated: 00000) or to the right or after the telephone number (formated: 000-000-0000)

So in each case, I would like to ge the days and time, ie:
"Monday - Friday - 3:00 - 10:00PM and Saturday & Sunday -12:00 - 8:00PM"

I would appreciate some help.

Best,

preg_match('/(\d{3}-\d{3}-\d{4}|\d{5})(.*)/s', $subject)

But this is flawed. Can't you just take the last line?

Hi pritaeas,

thanks for the reply! But the expression grabs everything from either the phone number or zipcode to the end of the line; whereas, I need the expression to grab everything after either the phone number or zipcode and before the last double quote.

I do not believe I can just take the last line because this string is part of an array that looks like:

Array
    (
        [0] => Array
                (
                    [cName] => "Record Table Tennis Club
        (4/30/2014)"
                    [location] => "Kahler Hall5440 Old Tucker Row
        Columbia, MD  21044
                        Monday & Wednesday - 1:00 - 4:00PM and 6:00 to 9:30PM
                    "
                    [days_times] => '0'
                    [zip] => 21044
                    [state] => 'MD'
                    [contact_person] => "Edward xxxxx"
                    [contact_phone] => '301-xxx-1926'
                )
    )

Part of my code that looks into this array to extract the necessary info looks like:

  $arr[$i]['location'] = str_replace("'", "\'", $rowData[1]);
    //get playing days

 $pattern="/(\d{3}-\d{3}-\d{4}|\d{5})(.*)/s";

      if(preg_match($pattern, $rowData[1], $match) == 1) {
            $arr[$i]['days_times'] = "'$match[0]'";
        } else {
            $arr[$i]['days_times'] = "'0'";
        }

If you do print_r($match); you should see the part after the number. It should be in $match[2]

Thanks pritaeas!

$match[2] is indeed the location of the information I'm looking for.

It appears that if the street number in the address block contains 5 digits, the expression begins at that point inlieu of zip code or the phone number.

An Example of the an address with 5 digits:

 [7] => Array
        (
            [cName] => "Washington Table Tennis Center
"
            [location] => "Washington Table Tennis Center 18709 Mooney Dr.
Gaithersburg, MD  20877
                Monday - Friday - 10:00AM - 9:00PM & Saturday - Sunday - 9:00AM - 10:00PM
            "
            [days_times] => ' Mooney Dr.
Gaithersburg, MD  20877
                Monday - Friday - 10:00AM - 9:00PM & Saturday - Sunday - 9:00AM - 10:00PM
            "'
            [zip] => 20877
            [state] => 'MD'
            [contact_person] => "xxxx xxxx"
            [contact_phone] => '301-728-6889'
        )

$pattern="/(\d{3}-\d{3}-\d{4}|\d{5})(.*)/s"; would extract:

 Mooney Dr.
    Gaithersburg, MD  20877
                    Monday - Friday - 10:00AM - 9:00PM & Saturday - Sunday - 9:00AM - 10:00PM "

instead of:

Monday - Friday - 10:00AM - 9:00PM & Saturday - Sunday - 9:00AM - 10:00PM

Is it possible to construct the expression to look specifically after the phone number or the zipcode?

It appears that if the street number in the address block contains 5 digits, the expression begins at that point inlieu of zip code or the phone number.

Yes, I knew that.

Is it possible to construct the expression to look specifically after the phone number or the zipcode?

Perhaps. Are there always two spaces before the zip?

Perhaps. Are there always two spaces before the zip?

yes, it appears!

The table cell containing the address block looks like this:

<td width="60%">Holland Town Center<br>12330 James St. Ste H10<br>Holland, MI  49424<br><br><u>Directions</u>:<br>Near "The Lost City" &amp; across from the WOW Center. 7 Killerpsin tables &amp; Newgy 2050 Robot. Brian is an ITTF &amp; USATT Certified Coach. Club is available for rental by appointment.</td>

I think I have an alternative to removing the zip or phone within the

$arr[$i]['days_times']

block.

to extract the date and time, I was doing:

 $arr[$i]['location'] = str_replace("'", "\'", $rowData[1]);
    //get playing days
 $pattern="/(\d{3}-\d{3}-\d{4}|\d{5})(.*)/s";
      if(preg_match($pattern, $rowData[1], $match) == 1) {
            $arr[$i]['days_times'] = "'$match[0]'";
        } else {
            $arr[$i]['days_times'] = "'0'";
        }

I have modified code to:

//get playing days

    if(preg_match("/(\d{3}-\d{3}-\d{4}|\d{5})(.*)/s", $rowData[1], $match) == 1) {

       $arr[$i]['days_times'] = preg_replace('/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/','',"'$match[2]'");


   } 
   else 
   {
        $arr[$i]['days_times'] = "'not Available'";

    }

This may not be the prettiest approach, but it seems to be working...I would appreciate any thoughts!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.