dtdetu Posted November 12, 2008 Share Posted November 12, 2008 hello i get content of a page with curl i and i am trying to find if there is a "Your message has been sent to" in the page i use this code but it doesnt work. can u check this. if (preg_match("[Your message has been sent to\!]", $store)) { ... } else { ... } Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/ Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 Why not just use strpos? Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688372 Share on other sites More sharing options...
SuperBlue Posted November 12, 2008 Share Posted November 12, 2008 You need to account for the situation where something would be found before and after the string you are looking for. <?php $string = "hallo this an attempt at Matching This String which should return true"; if (preg_match("/^.*Matching This String.*/s", $string)) { echo 'Match Found!'; } else { echo 'No Match Found!'; } ?> The "s" modifier at the end simply makes the dot include newlines. The star after the dot (.) both allows for nothing to exist, and everything. I.e the characters allowed before and after the pattern to be matched, may be repeated 0 or more times. So in your case the pattern would be something like: <?php if (preg_match("/^.*Your message has been sent to .*/s", $string)) { echo 'Match Found!'; } else { echo 'No Match Found!'; } ?> Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688452 Share on other sites More sharing options...
SuperBlue Posted November 12, 2008 Share Posted November 12, 2008 Actually this would be easier: <?php $string = "hallo this an attempt at Matching This String which should return true"; if (preg_match("/Matching This String/", $string)) { echo 'Match Found!'; } else { echo 'No Match Found!'; } ?> Removing the start "^" indicator will allow you to do this. Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688476 Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 Or you could just use strpos() as I said above (in this case I've used stripos()). $string = "hallo this an attempt at Matching This String which should return true"; if(stripos($string, "matching this string") !== false){ echo "MATHCED"; }else{ echo "NOT MATCHED"; } Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688492 Share on other sites More sharing options...
dtdetu Posted November 12, 2008 Author Share Posted November 12, 2008 thanks i used stripos now it works good now, i need one more:) i didnt want to open new topic , my question is how can i get the Limoncafe from this with pregmatch all <span style='color:#CC6600;'>Your message has been sent to Limoncafe</span> preg_match_all('~<span style="color:#CC6600;">([^<]*)</span>~',$store,$matches); echo $matches[1]; this code gives me Array only . Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688514 Share on other sites More sharing options...
ddrudik Posted November 12, 2008 Share Posted November 12, 2008 $matches[1] is an array in that case, use print_r to view it. Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688518 Share on other sites More sharing options...
dtdetu Posted November 12, 2008 Author Share Posted November 12, 2008 ok but still i get nothing, i thing there is error in my regex what do u think Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688525 Share on other sites More sharing options...
ddrudik Posted November 12, 2008 Share Posted November 12, 2008 You have single quotes in your source and double quotes in your pattern. Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688530 Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 $text = "<span style='color:#CC6600;'>Your message has been sent to Limoncafe</span>"; preg_match("~<span style='color:#CC6600;'>Your message has been sent to (.*?)</span>~i", $text, $matches); echo $matches[1]; Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688533 Share on other sites More sharing options...
dtdetu Posted November 12, 2008 Author Share Posted November 12, 2008 thanks again ProjectFear both working fine now. Link to comment https://forums.phpfreaks.com/topic/132397-solved-preg-match-pattern/#findComment-688540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.