PHP function to pass text to valid url

with the following snippets of PHP you can transform any text strings into valid urls. Therefore, what the following function does is translate text characters, which are not allowed in urls, by others that are, thus achieving correct urls for any web page.

Note that the following snippet translates the part of the urls that goes after the domainwhat is known in web development as the uri.

function get_url_valid_text($string_in){
	$string_output=mb_strtolower($string_in, 'UTF-8');
	//caracteres inválidos en una url
	//traduccion caracteres válidos
	$repl=array('-','-','a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','ny','o','o','o','o','o','o','u','u','u','u','y','y','','' );
	$string_output=str_replace($find, $repl, $string_output);
	//más caracteres inválidos en una url que sustituiremos por guión
	$find=array(' ', '&','%','$','·','!','(',')','?','¿','¡',':','+','*','\n','\r\n', '\\', '´', '`', '¨', ']', '[');
	$string_output=str_replace($find, '-', $string_output);
	$string_output=str_replace('--', '', $string_output);
	return $string_output;


  • Examples of use transforming text into urls
  • Keys to pass from text strings to valid uris
  • Conclusions about getting urls from any text

Examples of use transforming text into urls

Here are several examples of use for the previous function. You will see how, even though the input contains special characters, the output of the function is a completely valid url and ready to use on a website.

//ejemplo 1
$texto = 'función PHP para traducir texto con caracteres especiales a urls válidas';
$uri_valida = get_url_valid_text( $texto ); //devuelve "funcion-php-para-traducir-texto-con-caracteres-especiales-a-urls-validas"

//ejemplo 2
$texto2 = 'la araña de google puede visitar esta PÁGINA';
$uri_valida2 = get_url_valid_text( $texto2 ); //devuelve "la-arana-de-google-puede-visitar-esta-pagina"

Keys to pass from text strings to valid uris

Here are the key points or functions of this small script to get uris from a text:

  1. Change the text to lowercase but respecting that it may contain symbols and special characters. For this, the UTF-8 version of the php strtolower function is used, which is mb_strtolower( $text, $encoding ).
  2. compose a array with all those special symbols or characters that we know are common but are not allowed in a URL.
  3. create another array with the same number of positions with the translation that we want for each symbol.
  4. Replace one by another with the function str_replace from vanilla PHP.
  5. Create another array for unsupported characters, but this time thinking about those that are replaced by spaces (in a url, hyphens are equivalent to a space).

Conclusions about getting urls from any text

I am sure that this function will be really useful for you, and we can say that It is a basic in the development of web applications where any user can manipulate urls, since they usually do not know the restrictions that exist. Also, it is common to make mistakes over time. Errors that would leave a page without working, producing an error that can cause, in the worst case, the loss of the page in Google.

For my part, I recommend you to add this function to get urls from text in php in a class of Helpers where you group all those functions related to URLS. For example in a class titled UrlHelpersto later call the function with a clear and easy to search name UtilHelpers::get_url_valid_text ( ‘my text’ );

Finally, if you have read this far I would like to ask you, did you like it? If so, I would really appreciate it if you shared this post about php and urls to support the creation of this type of free content and keep it reaching more people, thank you.

Leave a Reply