【问题标题】:Use function preg_replace to replace period with paragraph使用函数 preg_replace 将句点替换为段落
【发布时间】:2016-05-04 22:34:10
【问题描述】:

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

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

所以实际上我想用 PARAGRAPH 和 PERIOD (.&lt;/p&gt;&lt;p&gt;) 替换 PERIOD

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

所以,我认为替换每三个 DOT 的解决方案对我来说都是救赎,所以不是每一个 DOT。

有什么想法吗?

感谢

编辑:

我更改了上面的代码并编辑了 wp-includes 文件夹中的核心 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 PATH,所以 something.com/wp-content/image.jpg 我有 - something.com/wp-content/image.

jpg

对此有什么想法吗?

注意:

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

【问题讨论】:

  • 失败的情况太多了,不值得这样改语法
  • @Dagon 好的,你还有什么建议吗?
  • 谢谢,但是在 12 万个帖子上使用人类有点病态,你不觉得吗?无论如何,我征求程序员的意见,而不是文案经理 :)
  • 一个想法:使用 HTML 解析器,然后只替换文本节点内容。
  • @IsmaelMiguel 你能详细说明一下吗?

标签: php string wordpress str-replace


【解决方案1】:

我需要更换每三个点,仅此而已

代码示例:

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

【讨论】:

  • 天哪,它就像一个魅力,有一些语法错误,但没有什么严重的。我只是将你的正则表达式合并到我的代码中,你真的很快:) 只有一个问题——我不知道你或这里的任何人对 wordpress 了解多少——我只需要在 POST 内容中实现它;现在我的图像不起作用 - 图像路径更改为:cdn.printfriendly.com/button-printgrnw20.

    png

  • 感谢您接受我的回答,现在我认为我有足够的 stackoverflow 点来发布 cmets :D 关于您提到的问题,一种可能的解决方案是您可以先检测内容是否为 URL ,例如: if (filter_var($string, FILTER_VALIDATE_URL)) { /* URL,不要这样做 / } else { / 不是 URL,这样做 */ }
  • 你能给我写一些虚拟代码示例吗?我之前没有使用图像过滤器?
  • @Dagon 我的博客中没有“M.D.”、“D.C.”、“...”、“$9.99”、“8:00 a.m”这类文字,所以我没有注定,一切都很好,比我想象的还要好; except - 如何从表达式中排除图像 URL
  • @OnlineSid 谢谢你。我尝试了完全相同的代码,带有一些插件 - 什么都没有;在这里也尝试了您的代码-同样的问题-图像无法正确加载。在每个图像 URL 期间都替换为 code

    code 所以每个图像 url 看起来像 link

【解决方案2】:

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

$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;
}

但是,正如 cmets 所指出的,这几乎不准确(例如,如果忘记了一个点)。短代码之所以流行是有原因的,那就是因为使用这些短代码更容易准确地操作字符串内容,当用作边界时也是如此。例如,你可以这样做:

$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;
}

【讨论】:

  • 我在上面编辑了我的问题,我知道语法问题,但这根本不是问题,代码就像一个魅力,但它也编辑了图像路径?!
  • 嗯,代码昨天工作,但今天不工作。什么都没有改变,但它不起作用。有什么想法吗?
  • 我现在使用了你的代码,因为我的代码不再工作了?!?!您有任何建议如何从@Tyblitz 上方的代码中排除网址
  • @Eager2Learn “不起作用”是什么意思?这段代码所做的只是在点上分割一个字符串,用p 标记替换每三个点,并将其他点保留为点。办公室。正如我提到的(以及任何基于这种算法的解决方案)“几乎不准确”。您的 URL 是否包含在 &lt;img&gt;&lt;a&gt; 标记中?
  • 你能帮我解决这个图像问题吗?
【解决方案3】:

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

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 问题,我不建议使用我的问题中解释的问题方法。感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多