Extract data PHP string
Asked Answered
Y

2

2

I have used file_get_contents() to basically get the source code of a site into a single string variable.

The source contains many rows that looks like this: <td align="center"><a href="somewebsite.com/something">12345</a></td>

(and a lot of rows that don't look like that). I want to extract all the idnumbers (12345 above) and put them in an array. How can I do that? I assume I want to use some kind of regular expressions and then use the preg_match_all() function, but I'm not sure how...

Yemen answered 20/4, 2011 at 19:42 Comment(2)
we would have to see the dataMakowski
Oh good Google, not another one. #1732848Remillard
C
1

Try this:

preg_match('/>[0-9]+<\/a><\/td>/', $str, $matches);
for($i = 0;$i<sizeof($matches);$i++)
 $values[] = $matches[$i];
Concertgoer answered 20/4, 2011 at 20:1 Comment(1)
Thanks! This gave me a basic idea, I went with preg_match_all('/[0-9]+<\/a><\/td>/', $html, $matches); return $matches[0]; Works perfetly!Yemen
V
4

Don't mess with regular expressions. Get the variable and let a DOM library do the mundane tasks for you. Take a look at: http://sourceforge.net/projects/simplehtmldom/

Then you can traverse your HTMl like a tree and extract stuff. If you really want to get funky, read up on xPath.

Valenta answered 20/4, 2011 at 19:49 Comment(0)
C
1

Try this:

preg_match('/>[0-9]+<\/a><\/td>/', $str, $matches);
for($i = 0;$i<sizeof($matches);$i++)
 $values[] = $matches[$i];
Concertgoer answered 20/4, 2011 at 20:1 Comment(1)
Thanks! This gave me a basic idea, I went with preg_match_all('/[0-9]+<\/a><\/td>/', $html, $matches); return $matches[0]; Works perfetly!Yemen

© 2022 - 2024 — McMap. All rights reserved.