...

View Full Version : String Replace - Multiple Rules?



wilcosky
09-19-2009, 12:25 AM
I don't know how to describe this so my subject may be a bit confusing.

I want to use the str_replace function but for multiple variables. For example, if some submits €, I want it to be replaced with € but also if they submit a I want it to be replaced with £. So I have the simple code below which does the job for euros. How can I add pounds and maybe even a couple other rules to this string?


str_replace(chr(128), "€", stripslashes($cost))

Fumigator
09-19-2009, 12:26 AM
The parameters can be arrays so you can replace multiple strings in one call.

http://us2.php.net/manual/en/function.str-replace.php

wilcosky
09-19-2009, 12:47 AM
The parameters can be arrays so you can replace multiple strings in one call.

http://us2.php.net/manual/en/function.str-replace.php

So I get that I can use something similar to the code below... but the code below would replace the euro symbol and the pound smybol with the same html. How do I change the code below so that if its a euro character the euro html is used and if it's a pound character the pound html is use?



$currency = array("chr(128)", "chr(163)");
str_replace($currency, "€", $cost);


That code will replace the character with a euro no matter if it's a euro or a pound...


I want to say if cost has chr(128) anywhere in it, then change the chr(128) to € and if cost has chr(163) anywhere in it, then change chr(163) to £

Otherwise if I user inputs a € symbol, it just displays as a question mark. My page is encoded as utf-8. The only way the symbols will display correctly is if I change it to iso-8859-1, however, if I do that, then the symbols show up correctly but some apostrophes turn into weird characters... so no matter what encoding I use, something gets messed up.

The easiest way for me to fix is to leave the encoding utf-8 (because everything shows up correctly except certain symbols like euros) but change certain symbols to the pure html form when the page is viewed by either using string replace or by creating some type of if else statement maybe...

MattF
09-19-2009, 02:18 AM
Example:



$find = array(chr(128), chr(163));
$replace = array('€', '£');

$output = str_replace($find, $replace, $cost);

wilcosky
09-19-2009, 02:24 AM
Example:



$find = array(chr(128), chr(163));
$replace = array('€', '£');

$output = str_replace($find, $replace, $cost);


Perfect! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum