Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/179951-regular-expression-to-match-this-html/
Share on other sites

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?

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?

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";
      }
    

 

 

 

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.

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 :(

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...

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.