daveh33 Posted November 22, 2007 Share Posted November 22, 2007 $to = str_replace(" ", "+", $to1); This line works fine - but I want it to also replace other characters - I tried to add the below code but it just seems to skip it and nothing is replaced, including the spaces $to = str_replace(" ", "+", $to1); $to = str_replace("<", "%3C", $to1); $to = str_replace("#", "%23", $to1); $to = str_replace("?", "%3F", $to1); $to = str_replace(";", "%3B", $to1); $to = str_replace(":", "%3A", $to1); $to = str_replace("&", "%26", $to1); $to = str_replace("$", "%24", $to1); $to = str_replace("<", "%3C", $to1); $to = str_replace(">", "%3E", $to1); $to = str_replace("%", "%25", $to1); $to = str_replace("{", "%7B", $to1); $to = str_replace("}", "%7D", $to1); $to = str_replace("|", "%7C", $to1); $to = str_replace("@", "%40", $to1); $to = str_replace("=", "%3D", $to1); What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/78379-solved-multiple-str_replace/ Share on other sites More sharing options...
rajivgonsalves Posted November 22, 2007 Share Posted November 22, 2007 I see the variable names say $to1 = "some string"; following should be your code $to = str_replace(" ", "+", $to1); $to = str_replace("<", "%3C", $to); $to = str_replace("#", "%23", $to); $to = str_replace("?", "%3F", $to); $to = str_replace(";", "%3B", $to); $to = str_replace(":", "%3A", $to); $to = str_replace("&", "%26", $to); $to = str_replace("$", "%24", $to); $to = str_replace("<", "%3C", $to); $to = str_replace(">", "%3E", $to); $to = str_replace("%", "%25", $to); $to = str_replace("{", "%7B", $to); $to = str_replace("}", "%7D", $to); $to = str_replace("|", "%7C", $to); $to = str_replace("@", "%40", $to); $to = str_replace("=", "%3D", $to); echo $to; also you can use urlencode function in php for the same I think Quote Link to comment https://forums.phpfreaks.com/topic/78379-solved-multiple-str_replace/#findComment-396606 Share on other sites More sharing options...
Zane Posted November 22, 2007 Share Posted November 22, 2007 there's htmlspecialchars() htmlentities() little easier than using mutliple str_replaces EDIT yeah and urlencode...I knew I forgot one beat me to the punch Quote Link to comment https://forums.phpfreaks.com/topic/78379-solved-multiple-str_replace/#findComment-396607 Share on other sites More sharing options...
nuxy Posted November 22, 2007 Share Posted November 22, 2007 Well, you have to use use the previous variable and replace occurences within that string/variable. What you currently have is replacing one character, and then when it comes to the next str_replace function, just overwriting it. You can also use array's with str_replace, I don't know if you noticed that? <?php $find = array(" ","<","#","?",";",":","&","$","<",">","%","{","}","|","@","="); $replace = array("+","%3C","%23","%3F","%3B","%3A","%26","%24","%3C","%3E","%25","%7B","%7D","%7C","%40","%3D"); $to = srt_repalce($find, $repalce, $to1); ?> Also, you do know of the function called urlencode? Quote Link to comment https://forums.phpfreaks.com/topic/78379-solved-multiple-str_replace/#findComment-396609 Share on other sites More sharing options...
daveh33 Posted November 22, 2007 Author Share Posted November 22, 2007 many thanks - I got it working using mutliple str_replaces - but seeing the other ways, its not the best way. I haven't used the str_replace function before so didn't know you could use arrays - I tested that and it works the same which a much shorter code. Never used or heard of the urlencode before Quote Link to comment https://forums.phpfreaks.com/topic/78379-solved-multiple-str_replace/#findComment-396616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.