PHP Regex删除最后一段和内容 [英] PHP Regex to remove last paragraph and contents

查看:77
本文介绍了PHP Regex删除最后一段和内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL表中存储了以下内容:

<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p><div class="item"><p>Some paragraph here</p><p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p><p>This is the paragraph I am trying to remove with regex.</p></div>

我正在尝试删除表中每一行的最后一个段落标签和内容.我可以很容易地用PHP遍历表,但是正则表达式让我感到困惑.

我在stackoverflow上发现的每一个preg_match都会给我一个"preg_match():未知修饰符"错误,或者var_dump显示一个空数组.我相信即使它确实起作用也只能匹配内容,所以我认为我需要preg_replace吗?

行的长度并不相同,但它始终是我要完全删除的最后一段.

如果有人可以告诉我怎么做,我将不胜感激.谢谢

解决方案

这将删除最后一个<p>anything</p>.

<?php
$html = '<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p><div class="item"><p>Some paragraph here</p><p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p><p>This is the paragraph I am trying to remove with regex.</p></div>';
$html = preg_replace('~(.*)<p>.*?</p>~', '$1', $html);
echo $html;

(.*)正在抓取所有内容,直到最后一个段落标签并将其存储. .*?抓住段落标记之间的所有内容,?告诉它在下一个结束的段落标记处停止.我们不在这里使用捕获,因为我们不在乎里面有什么. $1是最后一个<p>之前找到的内容. ~是定界符,用于说明正则表达式的开始和结束位置.我怀疑这是导致您的正则表达式当前失败的原因. http://php.net/manual/zh/regexp.reference.delimiters. php

输出:

<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p><div class="item"><p>Some paragraph here</p><p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p></div>

注意:您应该考虑使用XML/HTML解析器,因为带有HTML/XML的正则表达式会很快变得混乱.

http://php.net/manual/en/refs.xml.php
如何在PHP中解析和处理HTML/XML?

演示: http://sandbox.onlinephpfunctions.com/code/0ddf46c328323e8b6357313a5464733ff797bc3fI have the following stored in a MySQL table:

<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p><div class="item"><p>Some paragraph here</p><p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p><p>This is the paragraph I am trying to remove with regex.</p></div>

I'm trying to remove the last paragraph tags and content on every row in the table. I can loop through the table with PHP easily enough, but the regex has me stumped.

Every preg_match I've found on stackoverflow either gives me a "preg_match(): Unknown modifier" error, or the var_dump shows an empty array. I believe that would only match the content even if it did work so I think I need preg_replace?

The rows aren't identical in length, but it is always going to be the last paragraph that I want to completely remove.

Would appreciate if someone could show me how. Thanks

解决方案

This would remove the last <p>anything</p>.

<?php
$html = '<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p><div class="item"><p>Some paragraph here</p><p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p><p>This is the paragraph I am trying to remove with regex.</p></div>';
$html = preg_replace('~(.*)<p>.*?</p>~', '$1', $html);
echo $html;

The (.*) is grabbing everything until the last paragraph tag and storing it. The .*? grabs everything between the paragraph tags, the ? tells it to stop at the next closing paragraph tag. We don't use the capturing here because we don't care what is inside. The $1 is the found content before the last <p>. The ~ are delimiters telling where the regex begins and ends. I suspect this is what is causing your regexs to fail currently. http://php.net/manual/en/regexp.reference.delimiters.php

Output:

<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p><div class="item"><p>Some paragraph here</p><p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p></div>

Note: There are XML/HTML parsers you should consider using them because regexs with HTML/XML can get very messy quickly.

http://php.net/manual/en/refs.xml.php
How do you parse and process HTML/XML in PHP?

Demo: http://sandbox.onlinephpfunctions.com/code/0ddf46c328323e8b6357313a5464733ff797bc3f

这篇关于PHP Regex删除最后一段和内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆