Using Smarty to strip P tags from my HTML
Asked Answered
H

3

5

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

Hedley answered 6/3, 2012 at 16:47 Comment(0)
M
12

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.

Marinate answered 6/3, 2012 at 17:3 Comment(0)
A
0

You can do this using the regex_replace modifier:

{$foo = '<p>hello world</p><p some-att="ribute">foo</p>'}
{$foo|regex_replace:'#<\s*/?\s*p(\s[^>]*)?>#i':' '|escape}
Apanage answered 6/3, 2012 at 17:45 Comment(0)
A
0

To remove the tags and it's contents use

{$content|regex_replace:'/(<(<(p)\b[^>]*>).*?(<\/\2>)/is':''}

to remove more tags simply add |tag Example to remove span and div tags as well

{$content|regex_replace:'/(<(<(p|div|span)\b[^>]*>).*?(<\/\2>)/is':''}
Astereognosis answered 24/6, 2024 at 0:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.