str_replace doesn't works on values coming from DB

hi,

I’m trying to apply a str_replace on values coming from DB but it doesn’t work correctly. though it works on values that doesn’t come from DB.

$name2 = str_replace(array("à", "á", "â", "ã", "ä", "å", "æ"),"a", $name);

above code shows ‘Petróczy Gábor’ => ‘Petróczy Gábor’ doesn’t replaces ‘a’

So is it the $name variable thats come from the database? - If so can you please post a bit more of your code so we can look at it?

here is my code that I’m using …

 $query=mysql_query("SELECT * FROM `test`");
 while ($res_array=mysql_fetch_array($query)) {
  $old1 = strval($res_array["field"]);
  $new1 = normalizeName($old1);
  echo "'" . $old1 . "' => '" . $new1 . "' <br>\
\
";
  echo "'" . $old2 . "' => '" . $new2 . "' <br>\
\
";
 }
    function normalizeName($name)
  {   
    $name2 = str_replace('á','a', $name);
    return $name2;
 } 

So the code you first provided was not actual code you were using at all and you thought we could fix it? :rolleyes:

Well I’ve edited your code to this and then run it through writecodeonline.com


$res_array['field'] = 'aáa';
$old1 = strval($res_array["field"]);
print "$old1<br>";
$new1 = normalizeName($old1);
echo "'" . $old1 . "' => '" . $new1 . "' <br>\
\
";

function normalizeName($name)
   {   
   $name2 = str_replace('á','a', $name);
   return $name2;
   }

It works fine for me. All I can say is that I’m not sure why you’re using strval as I can only assume you have names in the database and not php variables and secondly I removed a line with $old2 and $new2 which had not been assigned in the code you just provided.

Thanks for your reply,
actually, it works like that but if you get the $res_array[‘field’] from DB. just try to put a test data in fields/table and grab it via query and then preform this function … you will get my problem :slight_smile:

Then thats suggesting a potential problem with your database. You have just said yourself that my example works so clearly str_replace is not at fault. Something is wrong with the result coming from the database. You need to do a var_dump and post that here before we can help any further :wink:

Var_dump the rows one by one and post them here - maybe we can spot something.

OMG! I have tried your solution before telling you :slight_smile: data is coming from database correctly but function is not applying/working on special characters.

I was also thinking like you without giving it a try. so try it first and then come here for help :x

OMG? - IS there really any need to be sarcastic? - If you’re going to be like that then I won’t help any further.

You came here and posted a SAMPLE of how you were using str_replace which was not even your real code. You then posted your real code which I’ve tested and found to work. I’ve then asked you to use var_dump on each row from the database and post them here. You’re now OMG’ing me and attempting to make me look stupid when you from the very start have been misleading about your problem.

Either post the var_dumps or don’t ask for help on irrelevant source code which isn’t your actual code. All I am asking you for is debug output. There is absolutely no need to OMG me and try to make me look stupid. I am at least trying to help you and last time I checked I was the only one.

I have proven that str_replace is working and you agreed. If the data you are supplying to it is not being replaced then there is a fault with it. I would also suggest you right click and look at the source of the dumps in case there are any symbols there that the browser isn’t displaying to you.

After further consideration I am now unsubscribing from this topic. I will no longer assist you.

Sound to me like a problem with character sets. If the database is stored in UTF8 and the PHP file in ISO-8859-1 for example than the characters are different, even though the look the same.
Make sure you have the same charset everywhere and try it then.