Jump to content

Recommended Posts

$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?

Link to comment
https://forums.phpfreaks.com/topic/78379-solved-multiple-str_replace/
Share on other sites

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

 

 

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.