Question

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& " " 
Was it helpful?

Solution

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

OTHER TIPS

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

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top