Jump to content

RegEx model numbers


blommer

Recommended Posts

I'm trying to make a simple list of mac model numbers, but it's currently formated like this:

<span id="contentcenter_specs_externalnav_wrapper"> 
	  	  <span id="contentcenter_specs_externalnav_noflip_1"><font color="white">1</font></span> 
		  <span id="contentcenter_specs_externalnav_noflip_2"><a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.0.html">eMac</a> <a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.0.html">G4/1.0 (ATI)</a></span> 
		  <span id="contentcenter_specs_externalnav_noflip_3">A1001</span> 
</span> 
      
<span id="contentcenter_specs_externalnav_wrapper"> 
	  	  <span id="contentcenter_specs_externalnav_noflip_1"><font color="white">1</font></span> 
		  <span id="contentcenter_specs_externalnav_noflip_2"><a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.25.html">eMac</a> <a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.25.html">G4/1.25 (USB 2.0)</a></span> 
		  <span id="contentcenter_specs_externalnav_noflip_3">A1002</span> 
</span> 
      
<span id="contentcenter_specs_externalnav_wrapper"> 
	  	  <span id="contentcenter_specs_externalnav_noflip_1"><font color="white">1</font></span> 
		  <span id="contentcenter_specs_externalnav_noflip_2"><a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.42.html">eMac</a> <a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.42.html">G4/1.42 (2005)</a></span> 
		  <span id="contentcenter_specs_externalnav_noflip_3">A1003</span> 

 

I'd like it to look like this:

 

A1001

A1002

A1003

 

 

Can anybody give me some advice? Thanks.

Link to comment
Share on other sites

If you know it will always be between the <span></span> tag you can use "!<span .*?>(.*?)</span>!",$string, if you know it will be between only between span tag that has specific content you can add text to it like in the example you showed, you could match noflip_3: "!<span .*?noflip_3\">(.*?)</span>!",$string

Link to comment
Share on other sites

<?PHP
$input = '<span id="contentcenter_specs_externalnav_wrapper">
             <span id="contentcenter_specs_externalnav_noflip_1"><font color="white">1</font></span>
           <span id="contentcenter_specs_externalnav_noflip_2"><a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.0.html">eMac</a> <a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.0.html">G4/1.0 (ATI)</a></span>
           <span id="contentcenter_specs_externalnav_noflip_3">A1001</span>
   </span>
        
   <span id="contentcenter_specs_externalnav_wrapper">
             <span id="contentcenter_specs_externalnav_noflip_1"><font color="white">1</font></span>
           <span id="contentcenter_specs_externalnav_noflip_2"><a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.25.html">eMac</a> <a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.25.html">G4/1.25 (USB 2.0)</a></span>
           <span id="contentcenter_specs_externalnav_noflip_3">A1002</span>
   </span>
        
   <span id="contentcenter_specs_externalnav_wrapper">
             <span id="contentcenter_specs_externalnav_noflip_1"><font color="white">1</font></span>
           <span id="contentcenter_specs_externalnav_noflip_2"><a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.42.html">eMac</a> <a href="http://www.everymac.com/systems/apple/emac/stats/emac_1.42.html">G4/1.42 (2005)</a></span>
           <span id="contentcenter_specs_externalnav_noflip_3">A1003</span> ';

$pattern = '%<span id="contentcenter_specs_externalnav_noflip_3">(.+)</span>%';
if(preg_match_all($pattern, $input, $matches)) {
	echo implode("\n", $matches[1]);
} else { 
	echo 'Not found!';
}
?>

Works perfectly.

Link to comment
Share on other sites

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.