Jump to content

Using preg_match_all with php.


dragin33

Recommended Posts

Hi,

  I'm trying to pull some data off a dynamic web page with php implode code.  I am then attempting to run preg_match or preg_match_all to grab some data from the web page.  When i grab the web page with impolde it looks like this... (plus more)

 

<td  class="key"> EPS: </td>

<td  class="val">0.60</td>

(including the line break.)

 

What I need to do is pull out the "EPS" and the value of EPS which would be "0.60".  I am new to preg_match and even newer to regular expressions.  Could someone please help me out..  I can get it to match the EPS but I really don't know how to get the very next value (.60) without going through the rest of the code and pulling out random other numbers.

Link to comment
Share on other sites

With the contents in one string:

 

<pre>
<?php
$data = <<<DATA
<td  class="key"> EPS: </td>

<td  class="val">0.60</td>
DATA;

preg_match_all('%<td\s+class="key">([^<]+)</td>\s*<td\s+class="val">([^<]+)%', $data, $matches);
print_r($matches);
?>
</pre> 

Link to comment
Share on other sites

Thank you for your help. That works great! 

 

I have read some sites on regular expressions so i know what some of the special characters do but would you mind explaining the different parts of this expression so that I / we may learn from you :)  thanks

Link to comment
Share on other sites

Thank you for your help. That works great! 

 

I have read some sites on regular expressions so i know what some of the special characters do but would you mind explaining the different parts of this expression so that I / we may learn from you :)  thanks

 

In particular could you explain this part

[^<]+

Link to comment
Share on other sites

NODE                    EXPLANATION

----------------------------------------------------------------------

  <td                      '<td'

----------------------------------------------------------------------

  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or

                          more times (matching the most amount

                          possible))

----------------------------------------------------------------------

  class="key">            'class="key">'

----------------------------------------------------------------------

  (                        group and capture to \1:

----------------------------------------------------------------------

    [^<]+                    any character except: '<' (1 or more

                            times (matching the most amount

                            possible))

----------------------------------------------------------------------

  )                        end of \1

----------------------------------------------------------------------

  </td>                    '</td>'

----------------------------------------------------------------------

  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or

                          more times (matching the most amount

                          possible))

----------------------------------------------------------------------

  <td                      '<td'

----------------------------------------------------------------------

  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or

                          more times (matching the most amount

                          possible))

----------------------------------------------------------------------

  class="val">            'class="val">'

----------------------------------------------------------------------

  (                        group and capture to \2:

----------------------------------------------------------------------

    [^<]+                    any character except: '<' (1 or more

                            times (matching the most amount

                            possible))

----------------------------------------------------------------------

  )                        end of \2

----------------------------------------------------------------------

)                        end of grouping

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.