如何删除<;p>;使用php从HTML中获取标记及其内容

如何删除<;p>;使用php从HTML中获取标记及其内容,php,html,Php,Html,下面是我需要从中删除标记的文本 <p> Addiction, stress and subjective wellbeing</p> <p> The need and significance of traditional shop lot pavements in the context of town conservation in Malaysia</p> <p> The role of wage and benefit in

下面是我需要从中删除标记的文本

<p> Addiction, stress and subjective wellbeing</p> 
<p> The need and significance of traditional shop lot pavements in the context of town conservation in Malaysia</p> 
<p> The role of wage and benefit in engaging employee commitment</p>
上瘾、压力和主观幸福感 马来西亚城镇保护背景下传统铺地路面的需求和意义

工资和福利在员工承诺中的作用

我试过这个

$title= preg_replace('#<p>(.*?)</p>#', '', $title['result']);**
$title=preg_replace(“#(.*)”

,“$title['result]”)**

但是我仍然得到了
标记,有什么想法吗?

您必须使用此正则表达式来捕获
标记及其所有内容:

'/<p\b[^>]*>(.*?)<\/p>/i'
$title = "<div>Text to keep<p class='classExample'>Text to remove</p></div>";
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $title);
echo $result;
'/]*>(.*)/i'
捕获和删除标签及其所有内容的工作示例:

'/<p\b[^>]*>(.*?)<\/p>/i'
$title = "<div>Text to keep<p class='classExample'>Text to remove</p></div>";
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $title);
echo $result;
$title=“要保留的文本

要删除的文本”

; $result=preg_replace('/]*>(.*?)/i',''.$title); 回声$结果;
如果您想使用正则表达式来解析HTML,那么您将无法做到这一点


请在此处阅读有关您的更多信息:

如果您只需要删除所有标记,请使用。

只需删除p标记即可

$text=str_ireplace('<p>','',$text);
$text=str_ireplace('</p>','',$text);    
$text=str_-ireplace(“”,“$text”);
$text=str_-ireplace(“

”、“$text”);
echo”“。带标签($row[主内容])。"";
试试:

如果只想删除
标记

$html = "
<p> Addiction, stress and subjective wellbeing</p> 
<p> The need and significance of traditional shop lot pavements town</p> 
<p> The role of wage and benefit in engaging employee commitment</p>
";

echo strip_tags($html);
$text=”测试段落。

; 回音条标签($text); 回音“\n”; //允许p和标记 回音条标签($text,);
此代码删除一个p标签:

function parse($html) {
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $ps = $dom->getElementsByTagName('p');
    //die('<pre>'.print_r($ps,1).'</pre>');
    if ($ps->length === 1){
        $p = $ps->item(0);
        return $p->nodeValue;
    } else {
        return '';
    }
}
函数解析($html){
$dom=新的DOMDocument();
@$dom->loadHTML($html);
$ps=$dom->getElementsByTagName('p');
//模具(''.print_r($ps,1.'');
如果($ps->length==1){
$p=$ps->项目(0);
返回$p->nodeValue;
}否则{
返回“”;
}
}

$title=preg\u replace('%]*>\s*([([^[]+])].[/\2])\s*

%s','','$result['title']);是否正确在您的案例中使用此选项:“$code>$title=preg_replace('/]*>(.*?)/i',''$result['title'])如果你想去掉标签,在myu看来,这意味着你想保留标签的内容,而不是标签本身。为此,需要在preg_replace函数的第二个参数中添加\1:$result=preg_replace('/]*>(.*?)/i','\1',$title);不,我需要在删除p标记后将此数据保存到数据库中。我正在使用CKEDITORI。如果您这样做,则1。它只能捕获空的p标签,如
和;2.此标记的内容将保留。然后使用正则表达式-1,因为它不是最终解决方案。你必须使用下面答案中的正则表达式。@MarcoPanichi如果我给出的答案解决了提问者的问题,那么这会给你带来什么问题呢。如果使用上述解决方案无法解决您的问题,请在stackoverflow中提出您的问题,而不是否决我的答案。如果您希望删除特定标签而不删除内容,您可能会发现此
仅剥离标签
功能很有帮助:
function parse($html) {
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $ps = $dom->getElementsByTagName('p');
    //die('<pre>'.print_r($ps,1).'</pre>');
    if ($ps->length === 1){
        $p = $ps->item(0);
        return $p->nodeValue;
    } else {
        return '';
    }
}