<?php
$regex = <<<'REGEX'
~
<strong\s*> # Match strong open tag
( # Open group 1
(?: # Non-capturing group
(?:(?!</?strong\s*>).) # Match anything that's not strong tag (open/close)
| # Or
(?R) # Repeat the whole pattern
)* # Repeat the non-capturing group zero or more times
) # Close group 1
</strong\s*> # Match strong close tag
~xs
REGEX;
# The x modifier is for the fancy spacing en commenting
# The s modifier is to match newlines with dot "."
$output = preg_replace_callback($regex, function($m){
return '<strong>' . preg_replace('~</?strong\s*>~', '', $m[1]) . '</strong>';
}, $input);
echo $output; // w1 <strong>w2</strong> w3 w4 <strong>w5 w6</strong> w7
Warning: Undefined variable $input in /in/D6MPi on line 21
Deprecated: preg_replace_callback(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /in/D6MPi on line 19
Output for 8.1.0 - 8.1.28
Warning: Undefined variable $input in /in/D6MPi on line 21
Deprecated: preg_replace_callback(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /in/D6MPi on line 21
Output for 8.0.0 - 8.0.30
Warning: Undefined variable $input in /in/D6MPi on line 21