Welcome to WebmasterWorld Guest from 23.22.250.113

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

What is wrong with this regular expression? Newline causing problem.

The newline is throwing me off and I can't get around it.

     

carlos123

4:17 am on Feb 29, 2012 (gmt 0)

10+ Year Member



I have been beating my head against the proverbial wall with the following PHP regular expression and would appreciate any insight from anyone on what is wrong with it.

It's not returning what I would expect it to return.

Here is the PHP test code encasing the regular expression...


<?php
$data = '<p align="center"><b><strong><font color="#641433" size="5">Featured Wines</font></strong></b></p>
<p align="center"><b>January 2007 </b></p>';

preg_match('|Featured Wines<\/font><\/strong><\/p>(.*)<\/p>|m', $data, $matches);
print_r($matches);
?>


It captures nothing!

But I can't figure out why. If I move the capturing parantheses before the newline in the $data it captures just fine. The m modifier should capture the newline and everything else between the paragraph tags into $matches but...nothing!

What I am trying to do is capture the month and year and using the rest of the HTML as help in parsing that out of the HTML pages.

Any tips would be appreciated.

Thanks.

Carlos

carlos123

4:34 am on Feb 29, 2012 (gmt 0)

10+ Year Member



I figured it out!

I had left out a bold tag in the reg exp.

The working code is...


<?php
$data = '<p align="center"><b><strong><font color="#641433" size="5">Featured Wines</font></strong></b></p>
<p align="center"><b>January 2007 </b></p>';

preg_match('|Featured Wines<\/font><\/strong><\/b><\/p>\n<p align="center"><b>(.*) (.*)<\/b><\/p>|U', $data, $matches);
print_r($matches);
?>


Problem solved!

Carlos

g1smd

7:48 am on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I find improved clarity when I use a varible for $pattern, so the preg_match is for

preg_match($pattern, $data, $matches);

and $pattern should be like = '/<p> .... </p>/';

Notice the two slashes.

If the pattern contains slashes, use another delimiter like = '#<p> .... </p>#'
 

Featured Threads

Hot Threads This Week

Hot Threads This Month