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
https://forums.phpfreaks.com/topic/94627-using-preg_match_all-with-php/
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> 

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

[^<]+

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.