preg_replace - 删除

图像周围的标签

发布于 2024-11-30 16:20:07 字数 2261 浏览 0 评论 0原文

目前构建 WordPress 主题和 wpautop 功能使用起来相当痛苦。当谈到正则表达式时,我几乎是一个完全的菜鸟,我想知道是否有人可以帮助我。

假设我有:

<p><img src="img/something.jpg" width="1249124" height="20" alt="foo" /></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);

Currently building a WordPress theme and the wpautop function is quite a pain to work with. I'm pretty much a complete noob when it comes to regex, I was wondering if anyone can help me out.

Say I have:

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

How can I replace that and basically only get rid of the <p></p> tags. Bare in mind that there will be other content so I can't rule out all <p> tags entirely.


This is the content:

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

Here's the script:

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-12-07 16:20:07
$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) «*»
$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);

Edit: Here is the pattern explanation

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) «*»
假面具 2024-12-07 16:20:07

为什么前面的答案有这么多代码?

我可以想出一个更短的解决方案:

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

Why so much code on the previous answers ?

I can think in a much shorter solution:

$result = preg_replace('%<p>(<img .*?/>)</p>%i', '$1', $html);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文