There is no errors in the code you posted so your error must lie another place in your code
<?php
$name = "potato+tomato";
$x = str_replace('+', '%20', $name);
echo $x;
Output: potato%20tomato
题
I did a URL encode and spaces have been replaced by +.
I am trying to replace the + with %20 using str replace.
$x = str_replace('+', '%20', $name);
What is the syntax error here?
解决方案
There is no errors in the code you posted so your error must lie another place in your code
<?php
$name = "potato+tomato";
$x = str_replace('+', '%20', $name);
echo $x;
Output: potato%20tomato
其他提示
You might prefer using the url_decode() function.
That said, your code looks fine.