我正在尝试从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+?[^]]+\])
仅删除第一个标记。
答案 0 :(得分:6)
你可能想要使用这样的东西
$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首先引导我们完成整个过程然后每个人都发疯了。我想保留这个!
<强>解释强>
is
:i
表示匹配大小写不敏感,s
表示匹配新行与点.
\s*
:匹配空格0次或以上\[caption
:匹配[caption
[^]]*
:匹配除]
0次或更多次以外的任何内容\]
:匹配]
.*?\[/caption\]
:匹配任何内容,直到[/caption]
找到(并匹配[/caption]
)\s*
:匹配空格0次或以上答案 1 :(得分:1)
因为看起来你只想要字符串的开头,我不会使用正则表达式而是字符串函数:
$pos = stripos($your_string, '[caption');
$result = substr($your_string, 0, $pos);
答案 2 :(得分:0)
看起来你可以通过换行符来爆炸字符串,然后走第一行......
<?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.
答案 3 :(得分:0)
[caption]是shortcode的示例。您可以使用Wordpress删除所有短代码&#39; 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首先引导我们完成整个过程然后每个人都发疯了。我想保留这个!