Ive been trying to use preg_split
but it's not working very well, this is how I'm using it:
$html_str = '<span class="spanClass" rel="rel span">Text Span</span>';
$arrTemp = preg_split('/\<span class=\"spanClass\" rel=\"(.+?)\"\>(.+?)\<\/span\>/', $html_str);
So I would get this 2 '(.+?)'
variables into an array(span rel and Text Span)
.
I'm probably not thinking about it in the best possible way to solve my problem, but the fact is that my string will have more than one <span>
mixed with trash html and I need to separate only the <span>
content in an array. Any better ideas?
First of all preg_split is the wrong function, you really meant preg_match from the syntax of regex that you seem to be using.
Correct use would be:
$html = '<span class="spanClass" rel="foo bar">Text Span</span>';
preg_match("/<span.*rel=[\"']([^\"']+)[\"'][^>]*>([^<]+)<\/span>/", $html, &$A);
print_r($A);
This outputs:
Array
(
[0] => <span class="spanClass" rel="foo bar">Text Span</span>
[1] => foo bar
[2] => Text Span
)
So above uses preg_match; $A[0] contains the entire line $A[1] the rel= stuff and $A[2] the Text Span stuff.