Page 1 of 1

very simple regex not working

Posted: Sat Oct 13, 2007 7:14 am
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]

Posted: Sat Oct 13, 2007 7:59 am
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.

Posted: Sat Oct 13, 2007 8:13 am
by Zoxive
Simple typo (Almost noticeable, but link is wrong also) feyd meant, preg_match_all().

Posted: Sat Oct 13, 2007 8:15 am
by feyd
I actually meant both preg_match() and preg_match_all(). It is their shared behavior.

Posted: Sat Oct 13, 2007 8:27 am
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.

highly appreciate this

Posted: Sat Oct 13, 2007 11:36 am
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!!