tag and its content from HTML using php
Below is the text I need to remove tags from
Addiction, stress and subjective wellbeing
The need and signi
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);
echo "<div id=\"main_content\">" . strip_tags($row[main_content]) . "</div>";
You must 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;
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?
$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>');
just to remove p tags you can do this
$text=str_ireplace('<p>','',$text);
$text=str_ireplace('</p>','',$text);
If you just need to strip all the markup, go with strip_tags().