Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso
Last active April 15, 2021 10:19
Show Gist options
  • Save FernandoBasso/b21e6a31ae83fb0e3816 to your computer and use it in GitHub Desktop.
Save FernandoBasso/b21e6a31ae83fb0e3816 to your computer and use it in GitHub Desktop.
Urlize a string for pretty urls and SEO.
<?php
// Thanks to: http://cubiq.org/the-perfect-php-clean-url-generator
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;
}
var_dump(toAscii('co"r/aç ã\o'));
// string(9) "cor-ac-ao"
@trijulio
Copy link

Works amazing, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment