繁体   English   中英

在结束HTML标记之前添加缺少的标点符号

[英]Adding missing punctuations before a closing HTML tag

我的字符串是一个HTML文档。 我想在之前没有标点符号的HTML结束标记之前添加一个点。 标点符号是.,?!:我想使用preg_replace

<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和其他网站。

期望的结果是:

<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>

你并不需要遍历$tags那样的,我要么做一个implode| ,或者在这种情况下恰好是所有可能元素的规则。

$source = '<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 = addMissingPunctuation( $source );
echo $source;
function addMissingPunctuation( $input ) {
    return preg_replace("/[^,.;!?]\K<\/h[1-6]>/mi", ".$0", $input);
}

演示: https//3v4l.org/6dNV7

你还需要忽略元素之前的任何字符, \\K就是这样。 ${}用于PHP变量, $0是捕获组,如果将来用\\0编写它可能会更清楚。

正则表达式演示: https//regex101.com/r/xUvvuf/1/

(使用\\0 0。https://3v4l.org/jGZal的示例)

你可以采取的另一种方法是使用标点符号跳过所有元素,这会减少一些步骤。

https://regex101.com/r/xUvvuf/2/

[,.;!?]<\/h[1-6]>(*SKIP)(*FAIL)|<\/h[1-6]>

你也可以改变分隔符 ; 这是个人偏好。 如果你不介意逃避/ s你可以继续这样做,如果不是只交换前导和关闭/~

演示: https//regex101.com/r/xUvvuf/3/

preg_replace("~[^,.;!?]\K</h[1-6]>~mi"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM