- preg_replace_callback: documentation ( source)
<?php
$text1 = <<<EOD
<tr class="aaa">
<td class="bbb">
<p>
あああああ
</p>
</td>
<tr>
EOD;
$text2 = <<<EOD
<tr class="aaa">
<td class="bbb">
あああああ
</td>
<tr>
EOD;
echo "【RESULT 1】\n\n";
echo preg_replace_callback(
'@(<br[^>]*+>)|\s?<p>|</p>\s?|(?<=<t(?:r|d))[^>]*+(?=>)@',
function ($matches) {
return isset($matches[1]) ? '、' : '';
},
$text1
);
echo "\n\n【RESULT 2】\n\n";
echo preg_replace_callback(
'@(<br[^>]*+>)|\s?</?+p>\s?|(?<=<t(?:r|d))[^>]*+(?=>)@',
function ($matches) {
return isset($matches[1]) ? '、' : '';
},
$text2
);