I need to create a form for a website that asks for your address, city, state, and zip code, and when you click submit it will redirect you to a mapquest page that has already loaded directions from your address to a local business. Mapquest requires that there be no spaces, just plus signs. How can I write a script in PHP to replace something like “1234 Example Street” to “1234+Example+Street”?
I suggest using urlencode and htmlentities together, like this:
< ?php
$query_string = 'param1=' . urlencode( $param1 ) . '¶m2=' . urlencode( $param2 );
$query_string = htmlentities( $query_string );
?>
I hope that makes sense…
Edit:
Using the string replace function isn’t recommended for what you’re trying to do…php already has a built in function to do the work – so it doesn’t really make sense to make more work for yourself and for the server…
Use the htmlentities() function.
$myencodedstring = htmlentities(“1234 Example Street”);
Actually, you want to use the str_replace() function.
< ?php
$myString = "1234 Example Street";
$newStr = str_replace( " ", "+", $myString );
echo( $newStr );
?>
Words of Wisdom,
– Hex