Reputation: 534
I'm trying to pick out all the words within a pre-determined pattern, but it's not working:
$html = "<tooltip>Do</tooltip> you<tooltip>know</tooltip>";
I want preg_match_all to return
Array ( [0] => Array ( [0] => Do) [1] => Array ( [0] => know ) )
Using this pattern:
preg_match_all("/<tooltip ?.*>(.*)<\/tooltip>/", $html, $matches);
Instead it's returning:
Array ( [0] => Array ( [0] => Do youknow ) [1] => Array ( [0] => know ) )
I'm guessing it's my pattern that's wrong, but I don't know what?>
Any ideas?
Thanks
Upvotes: 0
Views: 76
Reputation: 1899
check this SO post why we don't use regex to parse html.
If you persist with using regex to extract html, then use the regex @Lee provided
<tooltip[^>]*>(.*?)</tooltip>
But it will fail for (and many others):
<tooltip attr="some > pretend > stuff">Do</tooltip> you<tooltip>know</tooltip>
The above may never happen to you. There aren't many guarantees in programming, but if there were wouldn't you take it. DomDocument gives you that guarantee with html. Your call
Upvotes: 0
Reputation: 402
Try this:
preg_match_all("/<tooltip>([^<]+)<\/tooltip>/is", $html, $out);
You will get the desired output but in $out[1] not in $out[0].
[1] => Array
(
[0] => Do
[1] => know
)
Upvotes: 0
Reputation: 5463
This isn't quite there, but the regex for picking out the data is working fine. Just the way it builds the array doesn't quite match what you're looking for. But with a bit of tweaking I'm sure you can figure it out
<?php
$html = "<tooltip>Do</tooltip> you<tooltip>know</tooltip>";
preg_match_all("~<tooltip>(.*?)<\/tooltip>~", $html, $matches);
print_r($matches);
foreach($matches[0] as $key => $value) {
$arr[] = $value;
}
print_r($arr);
?>
$arr then returns Array ( [0] => Do [1] => know )
which is closer to what you're looking for.
Upvotes: 1
Reputation: 10603
I'm no regex expert, i use Expresso to build something that works, but i'm not going to say its the best or most robust regex you could use.
This seems to work however
<tooltip[^>]*>(.*?)</tooltip>
so:
preg_match_all("/<tooltip[^>]*>(.*?)<\/tooltip>/", $html, $matches);
Upvotes: 0