Tagged: string RSS

  • dave

    dave 2:21 pm on March 24, 2010 Permalink | Reply
    Tags: , , string   

    One more way to slugify texts in php, without the need of the iconv() function:

        public static function slugify($text) {
    		$text = trim($text);
    		$text = str_replace(".", "", $text);
    		$text = self::removeAccents($text);
    		// replace non letter or digits by -
    		$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    		// trim
    		$text = trim($text, '-');
    		// lowercase
    		$text = strtolower($text);
    		// remove unwanted characters
    		$text = preg_replace('~[^-\w]+~', '', $text);
    		return $text;
    
    	}
    
    	public static function removeAccents($text) {
    		$text = htmlentities(utf8_decode($text));
    		$remove = array("&", "acute", "grave", "circ", "tilde", "uml");
    		$text = str_replace($remove, "", $text);
    		return $text;
    	}
    

    Put it inside an Utils class, and use it like

    $slug = Utils::slugify("nothingisclear is awesome");
    
     
  • reecoo

    reecoo 9:55 pm on March 23, 2010 Permalink | Reply
    Tags: , , string   

    I really don’t remember if i’ve written this or took it from someone on the web (if you recognize it, please let me know, or just know that i didn’t want to steal it :) ).

    Anyway today i was into slugs and i surprisingly found a php simple-and-working static method for converting strings into slugs and i felt like sharing it:

    /**
     * PHP "iconv" REQUIRED !!!
     * @param object $string
     * @param object $space [optional]
     * @return
     */
    
    public static function convertStringIntoSlug($string,$space="-") {  
    
    	if (function_exists('iconv')) {
            $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
        }  
    
        $string = strtolower(preg_replace("/[^a-zA-Z0-9 -]/","", $string));
        $string = str_replace(" ", $space, $string);
    
        return $string;
    
    }
    

    here i’ve already wrote about a good jquery alternative (written by Leo Caseiro) for doing this

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel