imagine a page Title string in any given language (english, arabic, japanese etc) containing several words in UTF-8. Example:
$stringRAW = "Blues & μπλουζ Bliss's ブルース Schön";
Now this actually needs to be converted into something thats a valid portion of a URL of that page:
$stringURL = "blues-μπλουζ-bliss-ブルース-schön"
just check out this link This works on my server too!
Q1. What characters are allowed as valid URL these days? I remember having seen whol arabic strings sitting on the browser and i tested it on my apache 2 and all worked fine.
I guesse it must become: $stringURL = "blues-blows-bliss-black"
Q2. What existing php functions do you know that encode/convert these UTF-8 strings correctl开发者_如何学Pythony for URL ripping them off of any invalid chars?
I guesse that at least:
1. spaces should be converted into dashes
-
@
and '&'?
3. converts all letters to lower case (or are capitcal letters valid in urls?)
Thanks: your suggestions are much appreciated!
this is solution which I use:
$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 URL's are not a problem, but if you want the text to be lowercase then simply add $text = strtolower($text);
at the end :-).
I would use:
$stringURL = str_replace(' ', '-', $stringURL); // Converts spaces to dashes
$stringURL = urlencode($stringURL);
$stringURL = preg_replace('~[^a-z ]~', '', str_replace(' ', '-', $stringRAW));
Check this method: http://www.whatstyle.net/articles/52/generate_unique_slugs_in_cakephp
pick the title of your webpage
$title = "mytitle#$3%#$5345"
;
simply urlencode it
$url = urlencode($title);
you dont need to worry about small details but remember to identify your url request its best to use a unique id prefix in url such as /389894/sdojfsodjf
, during routing process you can use id 389894 to get the topic sdojfsodjf .
Here is a short & handy 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 and so forth depending on the possible input
精彩评论