Skip to main content
Participating Frequently
November 19, 2006
Question

PHP strip_tags

  • November 19, 2006
  • 5 replies
  • 558 views
Hello,

I am trying to strip all html tags except the <p>, <strong> and <br> tags. I am running this line of code:

$desc = strip_tags($row_prd['prd_desc'], '<p> <strong> <i> <br> <br /> <BR>');

I dont get any errors but the only tag that is left remaining is the <strong> tag. Both the <p> and <br> tags are removed. I tried adding upper case <br> tags and the XHTML <br> tag but still, these tags are removed.

I tried running:

$desc = strip_tags($row_prd['prd_desc'], '<br>');

And this does not work either. How come the <br> and <p> tags are removed even though i am telling php not to remove them? I am running PHP 5.x

Any help is greatly appreciated.

Thanks!
This topic has been closed for replies.

5 replies

Inspiring
November 20, 2006
barbedwire103 wrote:
> David,
>
> I found the problem. All the <br> tags were entered as <br/> without a space
> between the /. By first doing a string replace and then strip tags, this fixed
> the problem.

Interesting.

> Just wanted to thank you for your help.

No problem. Thanks for reporting back what the problem was. It may be
worth logging a bug with PHP about it because <br/> is, strictly
speaking, perfectly valid.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Participating Frequently
November 20, 2006
David,

I found the problem. All the <br> tags were entered as <br/> without a space between the /. By first doing a string replace and then strip tags, this fixed the problem.

Just wanted to thank you for your help.

Brad
Inspiring
November 20, 2006
barbedwire103 wrote:
> Is it case sensitive? In the db, i will sometimes have <BR>, <br /> and <br>
> Do i need to specify each (i have tried this with no luck) or just one (tried
> each by themselves with no luck). Could it be something wrong with the install
> of PHP?

No, it's not case sensitive. I have just checked with a mixture of <BR>
and <br /> in a test page. They were both handled correctly by this:

echo strip_tags($text,'<p><br>');

I'm using PHP 5.2.0, but strip_tags() has been around for a long time,
so I doubt if it works differently in earlier versions.

I would suggest doing some tests with static text. When working with
database results, it can be difficult to know exactly what you're getting.

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Participating Frequently
November 19, 2006
David,

Thanks for the reply. The text that is stored in the db is HTML. I am using a WYSIWYG to convert input from the user into actual HTML (i have the interakt KTML extension). So i know that the <br> tag is there. I found that the <p> tags were working fine. I was thinking that perhaps the reason the <br> tag is removed is because it is not wrapped around anything (no closing tag)? I doubt this is why but this tag seems to be the only one i cannot keep.

Is it case sensitive? In the db, i will sometimes have <BR>, <br /> and <br> Do i need to specify each (i have tried this with no luck) or just one (tried each by themselves with no luck). Could it be something wrong with the install of PHP?

Thanks!
Inspiring
November 19, 2006
barbedwire103 wrote:
> Hello,
>
> I am trying to strip all html tags except the <p>, and <br> tags. I am
> running this line of code:
>
> $desc = strip_tags($row_prd['prd_desc'], '<p>
<br> <br /> <BR>');

I don't know whether the web forum to newsreader interface is causing
problems, but and are not HTML tags. To use strip_tags, you
supply the opening version only of each tag you want to preserve. So
<br> is correct, even for XHTML.

> I tried running:
>
> $desc = strip_tags($row_prd['prd_desc'], '<br>');
>
> And this does not work either. How come the <br> and <p> tags are removed even
> though i am telling php not to remove them? I am running PHP 5.x

That should work. However, I suspect that you may be confused by the way
that text is stored in a database. Unless you store the HTML tags with
the original record, there are no paragraph or <br> tags. Check to see
what $row_prd['prd_desc'] contains before you pass it to strip_tags().

--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/