使用函数preg_replace将句点替换为段落


Use function preg_replace to replace period with paragraph

我的WP帖子中有很多未格式化的文本;我试着使用这个代码:

function user_content_replace($content) {
    return str_replace('.','.</p><p>',$content);
}
add_filter('the_content','user_content_replace', 99);

所以事实上,我想用段落和PERIOD(.</p><p>)代替PERIOD

但我需要一点帮助:首先,这个代码取代了每个DOT,所以我所有的永久链接、图像路径等都被DOT取代了,这很糟糕,在这种情况下,web根本不起作用。

所以,我认为更换每三个DOT的解决方案将拯救我,所以不是每个DOT。

有什么想法吗?

Thanx

编辑:

我修改了上面的代码,并编辑了wp-includs文件夹中的核心post-template.php文件:

function the_content( $more_link_text = null, $strip_teaser = false) {
    $content = get_the_content( $more_link_text, $strip_teaser );
    $content = apply_filters( 'the_content', $content );
    $content = preg_replace('/([^''.]*''.[^''.]*''.[^''.]*){1}''.([^''.]*)/s', '$1.</p><p>$2',$content);
    echo $content;
}

现在还可以,大多数文本的语法都是正确的,现在的问题是上面的表达式更改了IMAGES路径,所以改为something.com/wp-content/image.jpg我有-something..com/wp-content.image.

jpg

对此有什么想法吗?

注意:

嗯,代码昨天有效,但今天不起作用。代码中没有任何更改,但它不起作用。有什么想法吗?

我需要更换每三个点,这就是全部

代码示例:

$content = "first.sec'nond.third.fourth.fifth>.sixth].seventh.";
$replacement = '$1.</p><p>$2';
echo preg_replace("/([^''.]*''.[^''.]*''.[^''.]*){1}''.([^''.]*)/s", $replacement, $content);
echo "'n";

上述代码的输出:

first.sec
ond.third.</p><p>fourth.fifth>.sixth].</p><p>seventh.

您可以通过一些手动字符串迭代轻松做到这一点:

$text = 'text.with.dots.text.with.dots.text.with.dots';
function user_content_replace($content) {
    $str_parts = explode('.', $content);
    $result = '<p>';
    for ($i = 0; $i < count($str_parts); $i++) {
      $result .= $str_parts[$i];
      if (($i + 1) % 3 === 0)
          $result .= '.</p><p>';
      else
          $result .= '.';
    }
    $result .= '</p>';
    return $result;
}

然而,正如评论所指出的,这很难准确(例如,如果忘记了一个点)。短代码之所以流行是有原因的,那是因为使用这些代码更容易准确地操作字符串内容,也可以用作边界。例如,你可以这样做:

$better = '[myTag]text.with.dots[/myTag][myTag]text.with.dots[/myTag][myTag]text.with.dots[/myTag]';
function user_content_replace2($content) {
    $result = preg_replace('/'[myTag']/', '<p>', $content);
    $result = preg_replace('/'['/myTag']/', '</p>', $result);
    return $result;
}

这是我的问题的正确且有效的答案:

function user_content_replace($content) {
$sentences_per_paragraph = 3; // settings
$pattern = '~(?<=[.?!…])'s+~'; // some punctuation and trailing space(s)
$sentences_array = preg_split($pattern, $content, -1, PREG_SPLIT_NO_EMPTY); // get sentences into array
$sentences_count = count($sentences_array); // count sentences
$output = ''; // new content init
// see PHP modulus
for($i = 0; $i < $sentences_count; $i++) {
    if($i%$sentences_per_paragraph == 0 && $i == 0) { //divisible by settings and is first
        $output .= "<p>" . $sentences_array[$i] . ' '; // add paragraph and the first sentence
    } elseif($i%$sentences_per_paragraph == 0 && $i > 0) { //divisible by settings and not first
        $output .= "</p><p>" . $sentences_array[$i] . ' '; // close and open paragraph, add the first sentence
    } else {
        $output .= $sentences_array[$i] . ' '; // concatenate other sentences
    }
}
$output .= "</p>"; // close the last paragraph
echo $output;
}
add_filter('the_content','user_content_replace', 99);

上面的答案只是部分的,不适合WordPress的代码层次结构。此外,他们造成了图像URL问题,我不建议在我的问题中解释这种方法的问题。谢谢大家的帮助。