homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
What is wrong with this regular expression? Newline causing problem.
The newline is throwing me off and I can't get around it.
carlos123




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

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




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

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




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

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>#'

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved