I have some strings:
$string1 = '<p><strong>Extract me</strong></p><p>Leave me</p>';
$string2 = '<strong>Extract me</strong>Leave me';
$string3 = '<span style="font-weight: bold">Extract me</span><br /><span>Leave me</span>';
Let’s check $string3:
The first tag of the string is <span>
So the text between the first <span> and the first </span> wants to be extracted.
Extracted shall mean: remove it from $stringX and save it into $extractedX
How would I do this?
What you should do is use an assertion.
[^>]*?searches for any character that is NOT a>. This should be fine since if you need to use>as text, it would need to be escaped as>. Then it searches for the first closing tag as denoted by<\/.*>. The(?=)around it tells the regex engine not to include it in the match.http://regexr.com?30pkm