View Full Version : str_replace not giving correct output

08-11-2008, 03:24 PM
I have a database of text to output to a webpage.

The text could contain characters such as and as it is copied straight from brochures.

When I output the text to a browser it appears as a square: �

obviously its because the characters are not in HTML format, therefore i added a function to sort this out.

$description = "output from database";
$replace = array("", "", "");
$with = array("®","™", "°");

print str_replace($replace, $with, $description);

This works fine on my localhost but when i try it on my remote server it outputs the HTML as ® and ™
obviously i don't want the "" as again it shows up as a square!

Any idea why its doing this?

08-11-2008, 04:00 PM
rather than do this check on the output, I would suggest that you do the check on input into the database, that way when you output, it is already in the proper format.

Now as for why you are receiving incorrect output, it is possible that your host / browser doesn't support the word version of the replacement. Try to look up the unicode number and use that instead. EG the trade mark is ® as opposed to ®

http://www.tntluoma.com/sidebars/codes/ is a good resource.

08-11-2008, 10:28 PM
function clean_textarea($i)
if (!get_magic_quotes_gpc())
$i = addslashes($i);
$i = rtrim($i);
$look = array('&', '#', '<', '>', '\'', '(', ')', '"'); //"
$safe = array('&amp;', '&#35', '&lt;', '&gt;', '&#39', '&#40;', '&#41;', '&quot;');
$i = str_replace($look, $safe, $i);
return $i;

$data = $_POST['textarea'];
$insert = clean_textarea($data);

then put $insert in the query where $data would be

08-14-2008, 05:16 PM
I would use the Unicode, but try encoding your page as UTF-8 (basically Unicode). That should eliminate the need for this completely.
BTW, after 'as a square: ,' I see a question mark in a diamond, not a square. :p