views:

127

answers:

3

I would like to know If I can use a php function on a variable that contains delimiters like ' or ". I'm receiving a text content from my database and I would like to use: strip_tags($desc); on it but it does not work.

Here is an example of what it can contain using var dump:

string(1039) ""txt <a href=""/txt.php"" class=""txt"">txt</a> . txt'txt <a href=""/txt.php"" class=""txt"">txt</a> txtxtxtxt& " "
+2  A: 

I guess you want to remove all tags. You should use the builtin function strip_tags() instead.

soulmerge
I have tried too but it's only working when I try it on a variable with backslashs before the apostrophes. When I apply it directly on the content of the db it keeps displaying the html tags.
mnml
It doesn't matter if the string contains any quotes, so the error must be somewhere else. Could you post the input to strip_tags and its output, both using var_dump()?
soulmerge
Thanks I'm going to try that
mnml
Well never mind I guess thats not possible, and the answer is "it's retarded to have html code in a database"
mnml
A: 

Try not to use ereg_replace as it is going to be discontinued.

ereg_replace
This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

strip_tags
That said do you want to change all those chars to empty or are you trying to strip the tags? You can also convert the chars to the html_entities.

$desc = strip_tags($desc);
Frankie
A: 

I'm assuming you want to work on the variable, not strip out the tags, then use this:

<?php
$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);
?>

htmlentities, will make your ' & " safe to handle, then you can convert them back after if needed.

Reference for code: http://us2.php.net/manual/en/function.htmlentities.php

Jakub