Last active
December 15, 2015 13:09
-
-
Save AndruC/5265451 to your computer and use it in GitHub Desktop.
friendly_title ie slugify from FuelPHP Core class Inflector
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
/** | |
* Converts your text to a URL-friendly title so it can be used in the URL. | |
* Only works with UTF8 input and and only outputs 7 bit ASCII characters. | |
* | |
* @param string the text | |
* @param string the separator (either - or _) | |
* @return string the new title | |
*/ | |
public static function friendly_title($str, $sep = '-', $lowercase = false) | |
{ | |
// Allow underscore, otherwise default to dash | |
$sep = $sep === '_' ? '_' : '-'; | |
// Remove tags #AC - external class, but use your own utility function here | |
$str = \Security::strip_tags($str); | |
// Decode all entities to their simpler forms | |
$str = html_entity_decode($str, ENT_QUOTES, 'UTF-8'); | |
// Remove all quotes. | |
$str = preg_replace("#[\"\']#", '', $str); | |
// Only allow 7bit characters | |
$str = static::ascii($str); | |
// Strip unwanted characters | |
$str = preg_replace("#[^a-z0-9]#i", $sep, $str); | |
$str = preg_replace("#[/_|+ -]+#", $sep, $str); | |
$str = trim($str, $sep); | |
if ($lowercase === true) | |
{ | |
$str = \Str::lower($str); | |
} | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment