preg_replace - 删除图像周围的<p>标签</p>

时间:2011-08-20 07:26:35

标签: php regex preg-replace

目前正在构建一个WordPress主题和wpautop功能是非常痛苦的。在谈到正则表达式时,我几乎是一个完整的菜鸟,我想知道是否有人可以帮助我。

说我有:

<p><img src="img/something.jpg" width="1249124" height="20" alt="foo" /></p>

如何替换它,基本上只删除<p></p>标签。请记住,还有其他内容,因此我不能完全排除所有<p>标记。


这是内容:

<p><img src="http://85.17.31.78/~tfbox/wp-content/uploads/2011/08/spotify4.jpg" alt="foo" /></p>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
<p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>

这是脚本:

function ptags() {

    $c = get_the_content();

    echo preg_replace('#(.*?)<p>\s*(<img[^<]+?)\s*</p>(.*)#s', '$1$2$3', $c);

}

add_filter('the_content', 'ptags', 9);

2 个答案:

答案 0 :(得分:4)

$str = '<p><img src="img/something.jpg" width="1249124" height="20" alt="foo" /></p>';
$str = preg_replace('%(.*?)<p>\s*(<img[^<]+?)\s*</p>(.*)%is', '$1$2$3', $str);

编辑:这是模式说明

Match the regular expression below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the characters “<p>” literally «<p>»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 2 «(<img[^<]+?)»
   Match the characters “<img” literally «<img»
   Match any character that is NOT a “<” «[^<]+?»
      Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the characters “</p>” literally «</p>»
Match the regular expression below and capture its match into backreference number 3 «(.*)»
   Match any single character that is not a line break character «.*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

答案 1 :(得分:2)

为什么以前的答案会有如此多的代码?

我可以用更短的解决方案思考:

$result = preg_replace('%<p>(<img .*?/>)</p>%i', '$1', $html);