SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Title sanitizer

  1. #1
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Title sanitizer

    I'm looking for a function that cleans up post titles like wordpress does. So something like "How to 'correctly' use <b> tags" would turn into "how-to-correctly-use-b-tags".

    Anyone know where I could find such a function? I'm having bad luck with Google.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print preg_replace(array('/[^\w\-]+/''/-{2,}/'), '-'"How to ---'correctly' use <b> tags");
    # How-to-correctly-use-b-tags 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn. Didn't realize it could be so elegantly simple. Thanks a million, logic_earth!

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Your Welcome.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a slightly improved version, if anyone would like to use:

    PHP Code:
    function trim_characters($string$char_array) {
        
        if (!
    strlen($string))
            return 
    $string;
            
        if (!
    count($char_array))
            return 
    $string;
        
        
    // First char
        
    if (in_array(substr($string01), $char_array)) {
            
            
    $result substr($string1);
            
        } else {
            
            
    $result $string;
            
        }
        
        
    // Last char
        
    if (in_array(substr($result, -1), $char_array)) {
            
            
    $result substr($result0, -1);
            
        }
        
        if (
    $result != $string) {
            
            return 
    trim_characters($result$char_array);
            
        } else {
            
            return 
    $result;
            
        }
        
    }

    function 
    sanitize_title($string) {

        
    $cleaned preg_replace(array('/[^\w\-]+/''/-{2,}/'), '-'$string);
        
    $underscores_to_dashes str_replace('_''-'$cleaned);
        
    $trimmed trim_characters($underscores_to_dashes, array('-'));
        
        return 
    strtolower($trimmed);



    EDIT:
    revised
    PHP Code:

    function sanitize_title($string$backup='page') {

        
    $removed_apostrophes preg_replace('/(\w)\'(\w)/''$1$2'$string);
        
    $cleaned preg_replace(array('/[^\w\-]+/''/[-_]+/'), '-'$removed_apostrophes);
        
    $trimmed trim($cleaned'-');
        
    $final strtolower($trimmed);
        
        if (empty(
    $final))
            
    $final $backup;
        
        return 
    $final;


    Last edited by Haleden; Aug 18, 2007 at 10:12.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Improving your improved function :3
    PHP Code:
    function sanitize_title ($string) {
        
    $string preg_replace(array('/[^\w]+/''/[-_]+/'), '-'$string);
        
    $string trim($string'-');
        return 
    strtolower($string);

    trim can do the same thing your trim_characters function does.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Improving your improved function :3
    PHP Code:
    function sanitize_title ($string) {
        
    $string preg_replace(array('/[^\w]+/''/[-_]+/'), '-'$string);
        
    $string trim($string'-');
        return 
    strtolower($string);

    trim can do the same thing your trim_characters function does.
    Wow, I did not know that. lol.

    One final revision

    PHP Code:

    function sanitize_title($string$backup='page') {

        
    $removed_apostrophes preg_replace('/(\w)\'(\w)/''$1$2'$string);
        
    $cleaned preg_replace(array('/[^\w\-]+/''/[-_]+/'), '-'$removed_apostrophes);
        
    $trimmed trim($cleaned'-');
        
    $final strtolower($trimmed);
        
        if (empty(
    $final))
            
    $final $backup;
        
        return 
    $final;



  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whitelist filtering is often better and simpler than blacklist

    PHP Code:
    $title "  some --- 'title' ??? ";
    preg_match_all('/[a-z]+/'strtolower($title), $m);
    echo 
    implode('-'$m[0]); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •