blommer Posted June 13, 2010 Share Posted June 13, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/ Share on other sites More sharing options...
elitegosu Posted June 13, 2010 Share Posted June 13, 2010 if you know it will always start with A and it will be a four digit number then all you need it is preg_match("!A[0-9]{4}!",$string) Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/#findComment-1071578 Share on other sites More sharing options...
blommer Posted June 13, 2010 Author Share Posted June 13, 2010 Unfortunately it won't always start with A or be a 4 digit number. Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/#findComment-1071580 Share on other sites More sharing options...
elitegosu Posted June 13, 2010 Share Posted June 13, 2010 is there any criteria to match it by? will it always start with a single character followed by numbers? will the preceding span tag contain same content all the time? Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/#findComment-1071587 Share on other sites More sharing options...
elitegosu Posted June 13, 2010 Share Posted June 13, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/#findComment-1071599 Share on other sites More sharing options...
ignace Posted June 13, 2010 Share Posted June 13, 2010 libxml_use_internal_errors(true); $doc = new DomDocument(); if ($doc->loadHtmlFile('..url..')) { $xpath = new DomXPath($doc); foreach ($xpath->query('//span[@id="contentcenter_specs_externalnav_noflip_3"]') as $node) { echo $node->nodeValue, "<br>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/#findComment-1071604 Share on other sites More sharing options...
ZachMEdwards Posted June 18, 2010 Share Posted June 18, 2010 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/204672-regex-model-numbers/#findComment-1073937 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.