Results 1 to 4 of 4

Thread: str_replace not giving correct output

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default str_replace not giving correct output

    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.

    PHP Code:
    $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?

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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.

  3. #3
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Code:
    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

  4. #4
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    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.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •