Reputation: 2324
I am trying to make this work but I can't:
$str = "<html>1</html><html>2</html><html>3</html>";
$matches = array();
preg_match_all("(?<=<html>)(.*?)(?=</html>)", $str, $matches);
foreach ($matches as $d)
{
echo $d;
}
What I am doing wrong? The output must be:
123
Upvotes: -1
Views: 693
Reputation: 117298
You need to add delimiters (I've used !
) and you can skip the lookaheads/lookbehinds. Just make a capture group around the numbers you want. Nothing else.
Then look in the second matches
element for the array with the individual values.
Example:
preg_match_all("!<html>(.*?)</html>!", $str, $matches);
foreach ($matches[1] as $d)
{
echo $d;
}
Upvotes: 0
Reputation: 785156
This should work for you:
$str = "<html>1</html><html>2</html><html>3</html>";
preg_match_all("~(?<=<html>).*?(?=</html>)~", $str, $matches);
foreach ($matches[0] as $d) {
echo $d;
}
Output:
123
Changes are:
~
in preg_match_all
function pattern$matches[0]
in foreach
loop instead of $matches
$matches
before preg_match_all
callUpvotes: -1