Reputation: 555
I have a problem with a regex pattern. It returns two arrays as a result... Here is my code:
$code = preg_match_all("/\< style\>(.*?)\<\/style\>/",$code,$matches);
var_dump($matches);
As test I set:
$code = ">< xxxxx> try blah fooo blah < /xxxxx> idfidf oh < x>< /x> < style> blah blah blah style1 < /style>< style>blah blah style 2 x< /style>
It returns 2 arrays, I mean
$matches = array
0 => array
0 => string '< style> blah blah blah style1 < /style>' (length=38)
1 => string '< style>blah blah style 2 x< /style>' (length=34)
1 => array
0 => string ' blah blah blah style1 ' (length=23)
1 => string 'blah blah style 2 x' (length=19)
The matches I want are in the second array. I put space between the tags, because the editor not showing the HTML tags.
Upvotes: 0
Views: 3397
Reputation: 785156
Following code is working for me:
$code = "<xxxxx> try blah fooo blah </xxxxx> idfidf oh <x></x> <style> blah blah blah style1 </style><style>blah blah style 2 x</style>";
$code = preg_match_all("~<style>(.*?)</style>~si", $code, $matches);
var_dump($matches[1]);
s
is for DOT_ALL (including newline)i
is for ignore case matcharray(2) {
[0]=>
string(23) " blah blah blah style1 "
[1]=>
string(19) "blah blah style 2 x"
}
However just to let you know that parsing HTML from regex is not a very good idea, you would be better off using many HTML parsers available for php.
Upvotes: 1
Reputation: 86
How about using Xpath? http://nl.php.net/manual/en/class.domxpath.php
Works for me (most of the times, that is ;) )
Upvotes: 0
Reputation: 5695
Did you tried this:
echo $matches[0];
echo $matches[1]; // and so on, depend in the number of matches.
Upvotes: 1