Last active
April 15, 2021 10:19
-
-
Save FernandoBasso/b21e6a31ae83fb0e3816 to your computer and use it in GitHub Desktop.
Urlize a string for pretty urls and SEO.
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 | |
// 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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works amazing, thanks