Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Smarty to strip P tags from my HTML

I'm using this code {$entry.entry|strip_tags} to strip tags, however I would just like to strip <p> tags and not all HTML tags.

Can someone help?

Thank you

like image 821
michaelmcgurk Avatar asked Mar 06 '12 16:03

michaelmcgurk


People also ask

Is it possible to remove the HTML tags from data?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

How do I strip HTML tags in PHP?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter. Note: This function is binary-safe.

What is stripping in HTML?

HTML Stripper removes HTML tags and convert HTML code to text, which scrub text formatting of the HTML to save and share TEXT. HTML stripping is the process by which unnecessary HTML tags are removed from a web page. A common use case for this is when someone wants to publish an article on a website they don't control.


1 Answers

If you want to strip ONLY <p> tags, try a simple regular-expression replacement:

{$entry.entry|regex_replace:"/(<p>|<p [^>]*>|<\\/p>)/":""}

This will replace <p>, </p> and all <p many attributes> strings with an empty string.

Let me know if it works. I tested the regular expression in PHP, not directly in Smarty.

like image 69
lorenzo-s Avatar answered Oct 16 '22 09:10

lorenzo-s