dragin33 Posted March 6, 2008 Share Posted March 6, 2008 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 More sharing options...
effigy Posted March 6, 2008 Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94627-using-preg_match_all-with-php/#findComment-484904 Share on other sites More sharing options...
dragin33 Posted March 6, 2008 Author Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94627-using-preg_match_all-with-php/#findComment-485516 Share on other sites More sharing options...
dragin33 Posted March 6, 2008 Author Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94627-using-preg_match_all-with-php/#findComment-485540 Share on other sites More sharing options...
effigy Posted March 6, 2008 Share Posted March 6, 2008 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 https://forums.phpfreaks.com/topic/94627-using-preg_match_all-with-php/#findComment-485546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.