Skip to content

Instantly share code, notes, and snippets.

@sendyputra
Forked from chluehr/toAscii.php
Created December 14, 2015 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sendyputra/6e285504fd319bd4f2eb to your computer and use it in GitHub Desktop.
Save sendyputra/6e285504fd319bd4f2eb to your computer and use it in GitHub Desktop.
PHP clean url slug generator / converter (slug)
<?php
// source: http://cubiq.org/the-perfect-php-clean-url-generator
// author: Matteo Spinelli
// MIT License / http://creativecommons.org/licenses/by-sa/3.0/ (please re-check at source)
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;
}
// usage:
echo toAscii("Peux-tu m'aider s'il te plaît?", "'"); // French
// returns: peux-tu-m-aider-s-il-te-plait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment