【问题标题】:Regular Expression remove [caption]正则表达式删除 [标题]
【发布时间】:2013-06-03 23:50:27
【问题描述】:

我正在尝试从来自 Wordpress 生成的数据库的文本字符串中删除一些 html。

我想要这个:

Marnie Stanton led us through the process first and then everyone went crazy. 
[caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
[caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
[caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]  

变成这样:

Marnie Stanton led us through the process first and then everyone went crazy. 

所以我想要的是从第一个 [caption] 到最后一个 [/caption] 的所有内容。

我是从这个开始的:

(\[caption\s+?[^]]+\])

只删除第一个标签。

【问题讨论】:

  • Anne surprises herself ... 文本怎么样,应该在结果中吗?
  • 哦,实际上不应该是这样,但这只是如何将其添加到 wordpress 博客的问题,因此对于编写正则表达式的所有目的,它应该仍然存在。
  • 我把它从问题中删除了,所以也没有更多的混乱。

标签: php regex wordpress


【解决方案1】:

您可能想要使用类似的东西

$string = 'Marnie Stanton led us through the process first and then everyone went crazy. 
[caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
[caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
I want to keep this !
[caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]';

$new_string = preg_replace('#\s*\[caption[^]]*\].*?\[/caption\]\s*#is', '', $string);
echo $new_string;

输出:

Marnie Stanton 首先带领我们完成了整个过程,然后每个人都疯了。我想保留这个!

说明:

  • 修饰符isi表示不区分大小写,s表示用点匹配新行.
  • \s* : 匹配空格 0 次或多次
  • \[caption :匹配 [caption
  • [^]]* :匹配除 ] 之外的任何内容 0 次或更多次
  • \] :匹配 ]
  • .*?\[/caption\] :匹配任何内容,直到找到 [/caption](并匹配 [/caption]
  • \s* : 匹配空格 0 次或多次

Online demo

【讨论】:

    【解决方案2】:

    您似乎只想要字符串的开头,我不会使用正则表达式而是使用字符串函数:

    $pos = stripos($your_string, '[caption');
    $result = substr($your_string, 0, $pos);
    

    【讨论】:

    • 这将是解决这种特殊情况的一种方法,但是问题在于,这是一个 wordpress 网站,管理员可能会选择将需要显示的正文放在标题的末尾,而不是比开始。我只想删除它们所在的任何标题。
    • @DylanCross 这就是为什么我询问最初在字幕之间的文字...
    • 哦,对不起,我的评论一定被误解了。但那是我的错。
    【解决方案3】:

    [caption] 是shortcode 的示例。您可以使用 Wordpress 的 strip_shortcodes(); 功能删除所有简码。

    $text = 'Marnie Stanton led us through the process first and then everyone went crazy. 
    [caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
    [caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
    I want to keep this !
    [caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]';
    
    $text = strip_shortcodes($text);
    echo $text;
    

    这将输出:

    Marnie Stanton 首先带领我们完成了整个过程,然后每个人都疯了。我想保留这个!


    [caption] documentation

    strip_shortcodes documentation

    【讨论】:

      【解决方案4】:

      看来你可以用换行符来分解字符串,然后只取第一行......

      <?php
      
      $str = <<<EOD
      Marnie Stanton led us through the process first and then everyone went crazy.
      [caption id="attachment_76" align="alignnone" width="191"] One of the work stations[/caption]
      [caption id="attachment_78" align="alignnone" width="300"] The group is getting some great results[/caption]
      [caption id="attachment_83" align="alignnone" width="224"] You can see the prints multiplying[/caption]
      EOD;
      
      $lines = explode("\n", trim($str));
      
      echo $lines[0]; # Marnie Stanton led us through the process first and then everyone went crazy.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        • 2019-02-16
        • 2011-04-16
        • 2013-01-07
        • 1970-01-01
        相关资源
        最近更新 更多