What are the best practices for handling repetitive characters in a PHP string that will be used in a URI?

When handling repetitive characters in a PHP string that will be used in a URI, it is important to properly encode the string to ensure that it is URL-safe. This can be done using the urlencode() function in PHP, which will encode special characters in the string. By encoding the string before using it in a URI, you can avoid issues with characters that may be interpreted incorrectly by the server or browser.

$string = "example string with repetitive characters";
$encoded_string = urlencode($string);
echo $encoded_string;