Example 1 — Encode Text for a URL
Replace spaces and special characters with % codes.
<?php
$text = "Hello World & Co";
echo rawurlencode($text), PHP_EOL; How It Works
- Start with plain text. It has spaces and an
&. - Call rawurlencode(). A space becomes
%20. - The
&is encoded too. It becomes%26. - Use the result in a URL. It is now safe.

