Sickaaron
Sickaaron

Reputation: 448

Str_replace does nothing

I want to replace all £ to its HTML code £ => £ because it shows up as a ? in a diamond.

I am using the following code but it does nothing.

<?php
    include ("config.php");

    $get_id = mysql_real_escape_string($_GET['id']);
    $page_get = mysql_query("SELECT * FROM contents_page WHERE `id` = '$get_id'");
    while($page_row = mysql_fetch_assoc($page_get)) {
        $page_row["Page_Content"] = str_replace('£','&pound;',$page_row[Page_Content]);
        $smarty->assign('page_title',       $page_row['Page_Title']);
        $smarty->assign('page_content',     $page_row['Page_Content']);     
    }

    $smarty->display('page.tpl');
?>

I am using str_replace() but nothing happens its like it cannot see the £

Upvotes: 0

Views: 342

Answers (5)

Kzqai
Kzqai

Reputation: 23102

You'll probably need something like a self-implemented mb_str_replace(), assuming that the pound symbol is not within the standard ascii set.

There is are multiple example implementation on that page: http://php.net/manual/en/ref.mbstring.php

I -hope- that that's all you need, if you have to fully implement unicode it gets quite annoyingly complicated. It may be that you can avoid that if using the mb_str_replace contributed there doesn't work, you may have to turn unicode functionality on in the php ini in order for php to even recognize that the character you're trying to replace is a unicode one.

Upvotes: 0

Naveen Web Solutions
Naveen Web Solutions

Reputation: 735

<?php
$link1 = mysql_connect('localhost','user1','pass1',TRUE);

mysql_selectdb('db1',$link1);

mysql_set_charset('utf8',$link1);
?>

Upvotes: 0

Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

I had an encoding problem myself. Go to the database and see if the data is being garbled or not. if it is not, then set your connection attributes like this SET CHAR UTF8 before you connect to the database..

$link1 = mysql_connect('localhost','user1','pass1',TRUE); 


mysql_selectdb('db1',$link1); 

mysql_set_charset('utf8',$link2);

The other solution is to see to what the input was encoded before it went to the database, and then change the charset to that

mysql_set_charset('latin1',$link2);

for example.

Any way, what I would do, I would still set my chars to utf-8 , output the results to the page.. see if the character is actually a pound sound, if it is a different symbol, I would put it in the string replace function instead of the pound symbol

Upvotes: 1

deceze
deceze

Reputation: 522597

str_replace doesn't work for you because the encoding of the needle and the haystack are different and hence the character doesn't match. In fact, encoding is the only problem you need to care about, you don't need to replace anything. See http://kunststube.net/frontback for details on what you need to consider for encodings.

For some bigger picture background, also see What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.

Upvotes: 0

JvdBerg
JvdBerg

Reputation: 21866

Add this to the head tag of your page:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Upvotes: 1

Related Questions