Jump to content

preg_match question


elyfrank

Recommended Posts

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.

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

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.