'Get all texts between html tags in string [duplicate]

i have a string like that:

test test test <span> hello </span> test test test <span> hello2 </span>

and i try to get the text between the span tags like:

array [

0 => 'hello',
1 => 'hello2
];

i tried:

$regex = '#<\s*?span\b[^>]*>(.*?)</span\b[^>]*>#s';
$matches = [];
$item->text = preg_match($regex,  $item->text, $matches);

but there i only get the first match: 'hello'

php


Solution 1:[1]

What you need is:

$matches = [];
preg_match_all('/#<\s*?span\b[^>]*>(.*?)</span\b[^>]*>#s/', $input, $matches);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henryc17