php - modify (simplify) topic title for displaying in url -



php - modify (simplify) topic title for displaying in url -

i creating little message board in php , need "simplify" topic title display in url of topic.

examples:

ceci est united nations sujet d'exemple becomes ceci-est-un-sujet-d-exemple

j'ai été à la plage becomes j-ai-ete-a-la-plage

i dislike sp&cial character$ becomes i-dislike-spcial-character (if there improve translation, take it)

edit 1 : strings must utf-8 encoded

edit 2 : learned result of type of operation called "slug" or "friendly url"

here's i've done. think i've covered of "common" characters. obviously, it's going missing however.

/** * replaces special characters in string "non-special" counterpart. * * useful friendly urls. * * @param string * @return string */ function convertaccentsandspecialtonormal($string) { $table = array( 'À'=>'a', 'Á'=>'a', 'Â'=>'a', 'Ã'=>'a', 'Ä'=>'a', 'Å'=>'a', 'Ă'=>'a', 'Ā'=>'a', 'Ą'=>'a', 'Æ'=>'a', 'Ǽ'=>'a', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'ă'=>'a', 'ā'=>'a', 'ą'=>'a', 'æ'=>'a', 'ǽ'=>'a', 'Þ'=>'b', 'þ'=>'b', 'ß'=>'ss', 'Ç'=>'c', 'Č'=>'c', 'Ć'=>'c', 'Ĉ'=>'c', 'Ċ'=>'c', 'ç'=>'c', 'č'=>'c', 'ć'=>'c', 'ĉ'=>'c', 'ċ'=>'c', 'Đ'=>'dj', 'Ď'=>'d', 'Đ'=>'d', 'đ'=>'dj', 'ď'=>'d', 'È'=>'e', 'É'=>'e', 'Ê'=>'e', 'Ë'=>'e', 'Ĕ'=>'e', 'Ē'=>'e', 'Ę'=>'e', 'Ė'=>'e', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ĕ'=>'e', 'ē'=>'e', 'ę'=>'e', 'ė'=>'e', 'Ĝ'=>'g', 'Ğ'=>'g', 'Ġ'=>'g', 'Ģ'=>'g', 'ĝ'=>'g', 'ğ'=>'g', 'ġ'=>'g', 'ģ'=>'g', 'Ĥ'=>'h', 'Ħ'=>'h', 'ĥ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u', 'Ů'=>'u', 'Ű'=>'u', 'Ų'=>'u', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ũ'=>'u', 'ū'=>'u', 'ŭ'=>'u', 'ů'=>'u', 'ű'=>'u', 'ų'=>'u', 'Ŵ'=>'w', 'Ẁ'=>'w', 'Ẃ'=>'w', 'Ẅ'=>'w', 'ŵ'=>'w', 'ẁ'=>'w', 'ẃ'=>'w', 'ẅ'=>'w', 'Ý'=>'y', 'Ÿ'=>'y', 'Ŷ'=>'y', 'ý'=>'y', 'ÿ'=>'y', 'ŷ'=>'y', 'Ž'=>'z', 'Ź'=>'z', 'Ż'=>'z', 'Ž'=>'z', 'ž'=>'z', 'ź'=>'z', 'ż'=>'z', 'ž'=>'z', '“'=>'"', '”'=>'"', '‘'=>"'", '’'=>"'", '•'=>'-', '…'=>'...', '—'=>'-', '–'=>'-', '¿'=>'?', '¡'=>'!', '°'=>' degrees ', '¼'=>' 1/4 ', '½'=>' 1/2 ', '¾'=>' 3/4 ', '⅓'=>' 1/3 ', '⅔'=>' 2/3 ', '⅛'=>' 1/8 ', '⅜'=>' 3/8 ', '⅝'=>' 5/8 ', '⅞'=>' 7/8 ', '÷'=>' divided ', '×'=>' times ', '±'=>' plus-minus ', '√'=>' square root ', '∞'=>' infinity ', '≈'=>' equal ', '≠'=>' not equal ', '≡'=>' identical ', '≤'=>' less or equal ', '≥'=>' greater or equal ', '←'=>' left ', '→'=>' right ', '↑'=>' ', '↓'=>' downwards ', '↔'=>' left , right ', '↕'=>' , downwards ', '℅'=>' care of ', '℮' => ' estimated ', 'Ω'=>' ohm ', '♀'=>' female ', '♂'=>' male ', '©'=>' copyright ', '®'=>' registered ', '™' =>' trademark ', ); $string = strtr($string, $table); // currency symbols: £¤¥€ - dont bother them $string = preg_replace("/[^\x9\xa\xd\x20-\x7f]/u", "", $string); homecoming $string; } /** * create url title * * takes "title" string input , creates human-friendly url string. * * @param string * @param boolean * @return string */ if (!function_exists('friendlyurl')) { function friendlyurl($string, $lowercase = true) { $separator = '-'; if (function_exists('convertaccentsandspecialtonormal')) { $string = convertaccentsandspecialtonormal($string); } $trans = array( '/&\#\d+?;/i' => '', '/&\s+?;/i' => '', '/\.+/i' => '', '/\s+/' => $separator, '/\/+/' => $separator, '/[^a-z0-9\-\._]/i' => '', '/'. $separator .'+/' => $separator, '/'. $separator .'$/' => $separator, '/^'. $separator .'/' => $separator, '/\.+$/' => '' ); $string = strip_tags($string); $string = preg_replace(array_keys($trans), array_values($trans), $string); if ($lowercase === true) { $string = strtolower($string); } homecoming trim(stripslashes($string)); } }

php

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -