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.