kevinkhan Posted November 2, 2009 Share Posted November 2, 2009 Hi guys. Im looking for a regular expression to match html in this format <a class="fn repname_busName repkey_IE_600885_5" href="/Florists/Aisling_Flowers_Cork/IE_600885_5">Aisling Flowers Cork</a> $strListMatches = '!<a class="fn repname_busName repkey_IE_[0-9]*_[0-9]*" href="(.*)">(.*)</a>!isU'; but it doesnt seem to work. Can anyone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/ Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 That pattern matches that example. If it's not matching part of a site you're grabbing, the site doesn't have exactly the same pattern. More details required I think. Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949292 Share on other sites More sharing options...
kevinkhan Posted November 2, 2009 Author Share Posted November 2, 2009 Yes you were right sorry.. my fault.. What about this html code <li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="+353 (021)4501172" fileas="Abbey Business & Communications" -->(021)4501172<!-- sphoneid --></span></li> I want to extract the number (021)4501172 This is the regular expression i have at the moment $strContactMatches = '!<li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="(.*)" fileas="(.*)" -->(.*)<!-- sphoneid --></span></li>!isU'; but when i run the script i get an error Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '-' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\goldenPagesCrawler\original.php on line 10 What could the problem be? Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949326 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 You are using the exclamation mark (!) as a delimiter you are also using it as part of your pattern, you will need to change the delimiter or escape the exclamation marks (!) with a backslash. Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949331 Share on other sites More sharing options...
kevinkhan Posted November 2, 2009 Author Share Posted November 2, 2009 i changed it to.. strContactMatches2 = '~<li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="(.*)" fileas="(.*)" -->(.*)<!-- sphoneid --></span></li>~isU'; but its not getting any matches for this html code <li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="+353 (021)4501172" fileas="Abbey Business & Communications" -->(021)4501172<!-- sphoneid --></span></li> Is there something wrong with the regular expression now? Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949339 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 Seems to match fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949349 Share on other sites More sharing options...
kevinkhan Posted November 2, 2009 Author Share Posted November 2, 2009 i set up this script to extract the number on this page but im getting no mates? are you sure the regular expression is ok?? function getMatches($strMatch,$strContent) { if(preg_match_all($strMatch,$strContent,$objMatches)){ return $objMatches; } return ""; } $strListingUrl ="http://www.phpfreaks.com/forums/index.php/topic,275243.0.html"; $strContent = file_get_contents($strListingUrl); $strMatch = '~<li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="(.*)" fileas="(.*)" -->(.*)<!-- sphoneid --></span></li>~isU'; getMatches($strMatch,$strContent); if ($objMatches !=0) { echo '<pre>';print_r ($objMatches);echo '</pre>'; } else { echo "no matches"; } Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949376 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 That likely has nothing todo with your regular expression. if ($objMatches !=0) You don't assign objMatches a value at any point (at least at any point where the variable is in scope). I'm assuming you wish to use the value returned by the function, if thats the case your code should be... $objMatches = getMatches($strMatch,$strContent); if ($objMatches != 0) { echo '<pre>'; print_r ($objMatches); echo '</pre>'; } else { echo "no matches"; } Also since you are comparing against 0 you should really return 0 if there is no matches, not a blank string. Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949388 Share on other sites More sharing options...
kevinkhan Posted November 2, 2009 Author Share Posted November 2, 2009 i changed the code to this function getMatches($strMatch,$strContent) { if(preg_match_all($strMatch,$strContent,$objMatches)){ return $objMatches; } return ""; } $strListingUrl ="http://www.phpfreaks.com/forums/index.php/topic,275243.0.html"; $strContent = file_get_contents($strListingUrl); $strMatch = '~<li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="(.*)" fileas="(.*)" -->(.*)<!-- sphoneid --></span></li>~isU'; $objMatches = getMatches($strMatch,$strContent); if ($objMatches != 0) { echo '<pre>'; print_r ($objMatches); echo '</pre>'; } else { echo "no matches"; } But i still get no matches Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949392 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 Ok, I just realised the URL you are passing in is the URL of this thread. Why? This page will not contain HTML that will match your pattern. Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949395 Share on other sites More sharing options...
kevinkhan Posted November 2, 2009 Author Share Posted November 2, 2009 Sorry i taught it my match this line that im writing next <li class="tel"><strong>Tel: </strong><span><!-- sphoneid telnr="+353 (021)4501172" fileas="Abbey Business & Communications" -->(021)4501172<!-- sphoneid --></span></li> I taught it might pick out that line on this page and match it with the regular expression but obviously that html above is transformed to something else when i click post... Didnt think of that... Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949503 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 As you can tell, the code is syntax highlighted, this means there are all sorts of span tags to highlight it. Quote Link to comment https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/#findComment-949511 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.