내 문자열은 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>
내 기능 :
$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