hamza Posted December 4, 2011 Share Posted December 4, 2011 I need to get the content of this div. but getting nothing $str = "<div class='mainful'>this is testing of the string n thats it </div>"; $str = preg_quote($str, "/"); $result = preg_match_all("/^<div class=\/'mainful\/'>(.*?)<\/div>$/", $str , $matches_results); if($result){ print_r($matches_results); exit; } else { echo "no match "; } Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 4, 2011 Share Posted December 4, 2011 I changed what you used slightly but this will match both your class and the content of the div preg_match_all("|<div class='(.*?)'>(.*?)</div>|", "<div class='mainful'>this is testing of the string n thats it </div>", $out, PREG_PATTERN_ORDER); echo "<pre>"; print_r($out); I need to get the content of this div. but getting nothing $str = "<div class='mainful'>this is testing of the string n thats it </div>"; $str = preg_quote($str, "/"); $result = preg_match_all("/^<div class=\/'mainful\/'>(.*?)<\/div>$/", $str , $matches_results); if($result){ print_r($matches_results); exit; } else { echo "no match "; } Quote Link to comment Share on other sites More sharing options...
Adam Posted December 4, 2011 Share Posted December 4, 2011 @Drongo_III There's no need to pass the PREG_PATTERN_ORDER flag, it's the default. Though your pattern would match all DIVs with just a class attribute, so it's not really a practical solution. @hamza I think your problem is using ^ and $ at the start/end of the pattern. Together these require that the pattern is the *only* contents of the string. You can omit either or both to remove that restriction from one or both sides of the pattern. Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 5, 2011 Share Posted December 5, 2011 Well you could change it so it matches the class as a literal and then it would only target that div. Didn't know pattern_order was the default. I've always used it just for the sake of it. I've only used regular expressions for pretty basic stuff to be honest. Thanks for the tip @Drongo_III There's no need to pass the PREG_PATTERN_ORDER flag, it's the default. Though your pattern would match all DIVs with just a class attribute, so it's not really a practical solution. @hamza I think your problem is using ^ and $ at the start/end of the pattern. Together these require that the pattern is the *only* contents of the string. You can omit either or both to remove that restriction from one or both sides of the pattern. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.