very simple regex not working

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
thewebdrivers
Forum Commoner
Posts: 41
Joined: Fri Aug 17, 2007 3:32 pm
Location: india
Contact:

very simple regex not working

Post by thewebdrivers »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all

I know i am missing something very small but would require your help in spotting out.

here is a small piece of code that does not work.

Code: Select all

<?

function str_between($str,$start,$end) {
  if (preg_match_all('/' . preg_quote($start) . '(.*?)' . preg_quote($end) . '/',$str,$matches)) {
   return $matches[1];
  }
  // no matches
  return "ttt";
}

/*$url="http://store.apple.com/1-800-MY-APPLE/WebObjects/AppleStore.woa/wa/RSLID?mco=AA9C1548&nclm=CertifiedMac";
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
//curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
echo $data_mac = curl_exec ($ch);
curl_close ($ch); 
*/
$data_mac = file_get_contents("http://store.apple.com/1-800-MY-APPLE/WebObjects/AppleStore.woa/wa/RSLID?mco=AA9C1548&nclm=CertifiedMac");
//die();
$start='<td valign="top" class="details">';
$end='<p class="availability">';

echo $arr_data=str_between($data_mac,$start,$end);

foreach($arr_data as $var)
{
echo $var."<br>";
}



?>
the function returns false, however you can see that the pattern should match. Please help.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_match(_all) uses multiline mode by default. i.e. the result is must be on a single line (unless the pattern suggests otherwise.) Your pattern does not suggest otherwise. Adding the "s" pattern modifier should fix it.

Personally, I may use strpos() or stripos() in a loop for this myself.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Simple typo (Almost noticeable, but link is wrong also) feyd meant, preg_match_all().
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I actually meant both preg_match() and preg_match_all(). It is their shared behavior.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

feyd wrote:I actually meant both preg_match() and preg_match_all(). It is their shared behavior.
Yup, just saying your first sentence suggests preg_match uses multi line by default, when preg_match_all does. preg_match can with the s modifier like described in your post.
thewebdrivers
Forum Commoner
Posts: 41
Joined: Fri Aug 17, 2007 3:32 pm
Location: india
Contact:

highly appreciate this

Post by thewebdrivers »

I would like to thank you all (especially Feyd) for helping me out. I have been doing php/mysql for quiet a while but new to regex and your help is very much appreciated as it made my script work. Thanks a lot!!
Post Reply