How to convert a good page title into a valid URL string?

imagine a title bar in any language (English, Arabic, Japanese, etc.) containing a few words in UTF-8. Example:

$stringRAW = "Blues & μπλουζ Bliss ブルース Schön"; 

Now this needs to be converted to what is the real part of the URL of this page:

 $stringURL = "blues-μπλουζ-bliss-ブルース-schön" 

just check out this link This works on my server too!

Q1 . What characters are valid as valid URL these days? I remember that I saw Arabic strings sitting in the browser, and I tested them on my Apah 2, and everything worked perfectly.

I guess it should become: $stringURL = "blues-blows-bliss-black"

Q2. What existing php functions do you know that correctly encode / convert these UTF-8 strings for a URL, tearing them away from any invalid characters?

I guess at least: 1. spaces should be converted to a dash -
2. delete invalid characters? who are they? @ and '&'?
3. Converts all letters to lowercase (or letters to capital letters valid in URLs?)

Thank you: your suggestions are greatly appreciated!

+6
string url php url-rewriting
source share
5 answers

I would use:

 $stringURL = str_replace(' ', '-', $stringURL); // Converts spaces to dashes $stringURL = urlencode($stringURL); 
+6
source share

this is the solution i am using:

 $text = 'Nevalidní Český text'; $text = preg_replace('/[^\\pL0-9]+/u', '-', $text); $text = trim($text, "-"); $text = iconv("utf-8", "us-ascii//TRANSLIT", $text); $text = preg_replace('/[^-a-z0-9]+/i', '', $text); 

Capitals in the URL are not a problem, but if you want the text to be lowercase, just add $text = strtolower($text); to the end: -).

+10
source share
 $stringURL = preg_replace('~[^az ]~', '', str_replace(' ', '-', $stringRAW)); 

Check out this method: http://www.whatstyle.net/articles/52/generate_unique_slugs_in_cakephp

+2
source share

select the name of your web page $title = "mytitle#$3%#$5345" ; just urlencode this

 $url = urlencode($title); 

you don’t need to worry about small details, but do not forget to specify your url request to use a unique id prefix in the url, for example /389894/sdojfsodjf , during the routing process you can use id 389894 to get the sdojfsodjf theme.

+2
source share

Here is a short and comfortable one that does the trick for me

 $title = trim(strtolower($title)); // lower string, removes white spaces and linebreaks at the start/end $title = preg_replace('#[^a-z0-9\s-]#',null, $title); // remove all unwanted chars $title = preg_replace('#[\s-]+#','-', $title); // replace white spaces and - with - (otherwise you end up with ---) 

and of course you need to handle umlauts, currency signs, etc. depending on possible input

+1
source share

All Articles