elyfrank Posted April 6, 2012 Share Posted April 6, 2012 Hi guys, I hope you guys are having a terrific day. I am trying to get some data from a page using preg_match an place it in an array. This is the PHP code I got so far: <?php $url = "http://www.examplepage.html"; $raw = file_get_contents($url); preg_match("/<ul class='Codes'>(.*)<\/ul>/isU",$raw,$output); echo $output[1]; ?> I am able to read all text between <ul class='Codes'></ul> which is the one I am showing below. Result from PHP code above: <li><script type='text/javascript'>Code.number('4.236173.USD.EN.057880560561287921.386595955', '')</script><img src='/Images/providers/logos/Logo1.gif?cdn=040309' alt='' /> <script type='text/javascript'>Code.number('4.236173.USD.EN.057880560561287921.386595955', 'Read all codes from Number1')</script></li><li><script type='text/javascript'>Code.number('4.1957403.USD.EN.057880560561287921.458090474', '')</script><img src='/Images/providers/logos/logo2.gif?cdn=040309' alt='' /> <script type='text/javascript'>Code.number('4.1957403.USD.EN.057880560561287921.458090474', 'Read all codes from Number2')</script></li><li><script type='text/javascript'>Code.number('4.4801652.USD.EN.057880560561287921.1469771421', '')</script><img src='/Images/providers/logos/Logo6.gif?cdn=040309' alt='' /> <script type='text/javascript'>Code.number('4.4801652.USD.EN.057880560561287921.1469771421', 'Read all codes from Number3')</script></li> This is what I would like to do: Count how many times Code.Number happens. and then for each of then capture and place in array the code number between the ' '. (the first example will be 4.236173.USD.EN.057880560561287921.386595955) and so on. Code Numer 0,1 = 4.236173.USD.EN.057880560561287921.386595955 Code number 0,2= Logo1.gif Code number 1,1 = 4.1957403.USD.EN.057880560561287921.458090474 code number 1,2=Logo2.gif and so on. Thank you very much. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 7, 2012 Share Posted April 7, 2012 Well, you don't show the full content you are working with. It's quite possible you don't need the regex you already have - is there other content on the page with "Code.Number" that is outside the UL tags? if not, you just need one regex pattern for the page. preg_match_all("#Code.number\('([^']*)#", $text, $matches); print_r($matches[1]); Output: Array ( [0] => 4.236173.USD.EN.057880560561287921.386595955 [1] => 4.236173.USD.EN.057880560561287921.386595955 [2] => 4.1957403.USD.EN.057880560561287921.458090474 [3] => 4.1957403.USD.EN.057880560561287921.458090474 [4] => 4.4801652.USD.EN.057880560561287921.1469771421 [5] => 4.4801652.USD.EN.057880560561287921.1469771421 ) Quote Link to comment Share on other sites More sharing options...
elyfrank Posted April 7, 2012 Author Share Posted April 7, 2012 Thank you Psycho, it worked great. Quote Link to comment Share on other sites More sharing options...
elyfrank Posted April 7, 2012 Author Share Posted April 7, 2012 Psycho, I am sorry to bother you. How can I extract this text using preg_match_all? Where Read all reviews from is the beguining and ') is the end? 'Read all reviews from text-to-extract') I having trouble with the delimiters. This is what I got so far: preg_match_all("#Read all reviews from \('([^']')*)#", $text, $matches); but is not working. Thank you very much. Quote Link to comment Share on other sites More sharing options...
elyfrank Posted April 7, 2012 Author Share Posted April 7, 2012 Got it thanks 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.