Created
April 4, 2015 05:34
-
-
Save aamnah/59e43772b020faabcf44 to your computer and use it in GitHub Desktop.
Perfect Clean URL Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Perfect Clean URL Generator | |
| // Source: http://cubiq.org/the-perfect-php-clean-url-generator | |
| setlocale(LC_ALL, 'en_US.UTF8'); | |
| function toAscii($str, $replace=array(), $delimiter='-') { | |
| if( !empty($replace) ) { | |
| $str = str_replace((array)$replace, ' ', $str); | |
| } | |
| $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); | |
| $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); | |
| $clean = strtolower(trim($clean, '-')); | |
| $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); | |
| return $clean; | |
| } | |
| echo toAscii("Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->"); | |
| echo toAscii("Perché l'erba è verde?", "'"); // Italian | |
| echo toAscii("Peux-tu m'aider s'il te plaît?", "'"); // French | |
| echo toAscii("Tänk efter nu – förr'n vi föser dig bort"); // Swedish | |
| echo toAscii("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ"); | |
| echo toAscii("Custom`delimiter*example", array('*', '`')); | |
| echo toAscii("My+Last_Crazy|delimiter/example", '', ' '); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Perfect Clean URL Generator | |
| // Source: http://cubiq.org/the-perfect-php-clean-url-generator | |
| // DEMO | |
| setlocale(LC_ALL, 'en_US.UTF8'); | |
| function toAscii($str, $replace=array(), $delimiter='-') { | |
| if( !empty($replace) ) { | |
| $str = str_replace((array)$replace, ' ', $str); | |
| } | |
| $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); | |
| $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); | |
| $clean = strtolower(trim($clean, '-')); | |
| $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); | |
| return $clean; | |
| } | |
| $messed = "Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->"; | |
| echo '<strong>' . $messed . '</strong><br>'; | |
| echo toAscii($messed) . '<br/><br/>'; | |
| // Italian | |
| $italian = "Perché l'erba è verde?"; | |
| echo '<strong>' . $italian . '</strong><br>'; | |
| echo toAscii($italian, "'") . '<br/><br/>'; | |
| // French | |
| $french = "Peux-tu m'aider s'il te plaît?"; | |
| echo '<strong>' . $french . '</strong><br>'; | |
| echo toAscii($french, "'") . '<br/><br/>'; | |
| // Swedish | |
| $swedish = "Tänk efter nu – förr'n vi föser dig bort"; | |
| echo '<strong>' . $swedish . '</strong><br>'; | |
| echo toAscii($swedish) . '<br/><br/>'; | |
| // Special Characters | |
| $special = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ"; | |
| echo '<strong>' . $special . '</strong><br>'; | |
| echo toAscii($special) . '<br/><br/>'; | |
| echo '<strong>Custom`delimiter*example</strong><br>'; | |
| echo toAscii("Custom`delimiter*example", array('*', '`')) . '<br/><br/>'; | |
| echo '<strong>My+Last_Crazy|delimiter/example</strong><br>'; | |
| echo toAscii("My+Last_Crazy|delimiter/example", '', ' ') . '<br/><br/>'; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment