Reputation: 394
Below is the text I need to remove <p>
tags from
<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>
I tried this
$title= preg_replace('#<p>(.*?)</p>#', '', $title['result']);**
But still am Getting <p>
tags, any ideas?
Upvotes: 9
Views: 63663
Reputation: 13404
You should use this regular expression to catch <p>
tag and all of its content:
'/<p\b[^>]*>(.*?)<\/p>/i'
Working example to catch and remove <p>
tag and all of its content:
$title = "<div>Text to keep<p class='classExample'>Text to remove</p></div>";
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $title);
echo $result;
Please see live demo on Codepad
If you want to use regular expressions to parse HTML - then you will not be able to do this.
Read more about your matter here: How do you parse and process HTML/XML in PHP?
Upvotes: 22
Reputation: 1
$data1=html_entity_decode("<p>your text with p tag</p>");
$data2=str_ireplace('<p>',' ',$data1);
$data3=str_ireplace('</p>',' ',$data2);
echo $data3;
Upvotes: 0
Reputation: 87
Here is another example to strip all <p>
tags with or without the closing / after the first <
$content = '<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>';
$new_content = preg_replace('/<(|\/)p>/', '', $content);
dump($new_content);
die('HERE');
Upvotes: 0
Reputation: 589
This code removes one p-tag:
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 '';
}
}
Upvotes: 0
Reputation: 1555
$text = '<p>Test paragraph.</p> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow p and a tag
echo strip_tags($text, '<p><a>');
Upvotes: 2
Reputation: 14523
Try:
If you want to remove <p>
tag only
$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);
If you want to remove <p>
tags and its content
$html = preg_replace('#\<p>[{\w},\s\d"]+\</p>#', "", $html);
Upvotes: 7
Reputation: 866
echo "<div id=\"main_content\">" . strip_tags($row[main_content]) . "</div>";
Upvotes: 3
Reputation: 3165
just to remove p tags you can do this
$text=str_ireplace('<p>','',$text);
$text=str_ireplace('</p>','',$text);
Upvotes: 6
Reputation: 22817
If you just need to strip all the markup, go with strip_tags().
Upvotes: 2