私の文字列はHTMLドキュメントです。直前に句読点がない場合、HTMLの終了タグの前にドットを追加したい。句読点は.,?!:
です。私はそのためにpreg_replace
を使いたいと思います。閉じるHTMLタグの前に紛れた句読点を追加する
<p>Today, not only we have so many breeds that are trained this and that.</p>
<h4><strong>We must add a dot after the closing strong</strong></h4>
<p>Hunting with your dog is a blah blah with each other.</p>
<h2>No need to change this one!</h2>
<p>Hunting with your dog is a blah blah with each other.</p>
My機能:
$source = 'the above html';
$source = addMissingPunctuation($source);
echo $source;
function addMissingPunctuation($input) {
$tags = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ];
foreach ($tags as $tag) {
$input = preg_replace(
"/[^,.;!?](<\/".$tag.">)/mi",
".${0}",
$input
);
}
return $input;
}
私は.${0}
、.$0
、.${1}
、.$1
、.\\0
、.\\1
を試してみましたが、何も働きません。最高でも、それは試合を呑み込むが、それを何かに置き換えない。私のパターンのマッチング部分はregex101や他のサイトで動作するようです。
所望の結果は次のとおりです。あなたがそのような$tags
を反復処理する必要はありません
<p>Today, not only we have so many breeds that are trained this and that.</p>
<h4><strong>We must add a dot after the closing strong</strong>.</h4>
<p>Hunting with your dog is a blah blah with each other.</p>
<h2>No need to change this one!</h2>
<p>Hunting with your dog is a blah blah with each other.</p>
それは、クリスの仕事あなたは答えとしてそれを置く場合、私はそれをマークすることができます... ...それが理由を理解するために多くは、しばらくの間、私の頭を悩まれて感謝を分析しますソリューションとして – Lazhar