I am trying to look for a partnumbers in a string. Partnumber can contain any type of character.
so in
$html = '<td class="seller" style="padding:3px;">ABC1234<br>';
I want to output ABC1234.
I am using
preg_match_all("/<td class=\\"row2\\" style=\\"padding:3px;\\">(.*?)<br>/",$html,$data);
But it’s only returning
<td class=\\"row2\\" style=\\"padding:3px;\\">
How do I make $data return ABC1234?
well for starters your pattern doesnt match your input, so you wont get a result (Check the class attribute)
<?php
$html = '<td class="row2" style="padding:3px;">ABC1234<br>';
preg_match_all("/<td class=\\"row2\\" style=\\"padding:3px;\\">(.*?)<br>/",$html,$data);
print_r($data);
?>
for me, this code generated:
Array ( [0] => Array ( [0] => ABC1234
) [1] => Array ( [0] => ABC1234 ) )
StarLion:
<?php
$html = '<td class="row2" style="padding:3px;">ABC1234<br>';
preg_match_all("/<td class=\\"row2\\" style=\\"padding:3px;\\">(.*?)<br>/",$html,$data);
print_r($data);
?>
for me, this code generated:
Array ( [0] => Array ( [0] => ABC1234
) [1] => Array ( [0] => ABC1234 ) )
Oops! I had copies the wrong code. I’ve been looking at code for over 10 hours so my eyes were failing me. Thanks!
dyer85
April 23, 2010, 9:03am
6
If you’re parsing HTML, it’s best to avoid regexes. Instead, use the [DOM (e.g., [URL="http://php.net/manual/en/domdocument.loadhtmlfile.php"]DOMDocument::loadHTMLFile](http://php.net/dom)()).