SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Neat way to Strip URL?

    I am going to include title in the URL for SEO.

    However it need to strip illegal character such as ' : ] " / \ * and so on...

    What is the best way doing this?

    $a = "Hello? What's is your name?"

    should return: "hello-whats-is-your-name"

    This is what I have done, is any better way?

    Code PHP:
    function StandardizeString($PassedStr)
     {
     $PassedStr = preg_replace('/[\'!?"]/','', $PassedStr); // Eliminate all quotes
     $PassedStr = preg_replace('/[^a-zA-Z0-9]+/', '-', $PassedStr); // Remove all spaces
     $PassedStr = strtolower($PassedStr); // Lowercase everything
     return $PassedStr;
     }
     
    $a = "Hello, What's your name!???$%^&*()";
     
    echo StandardizeString($a);

    Output: hello-whats-your-name-
    [Home Sweet Home]

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    It might also be useful to have a quick trim($str, '-') to snip off leading/trailing dashes.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Leeds, UK
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this function to create 'tokens' of strings...

    function str2token($string) {
    $r = ereg_replace('[^[:alnum:]-]','-',strtolower($string));
    $r = preg_replace('/-+/','-',trim($r,'-'));
    return $r;
    }

  4. #4
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's mine:

    PHP Code:
    /**
     * Create URL Title
     *
     * Takes a "title" string as input and creates a
     * human-friendly URL string with either a dash
     * or an underscore as the word separator.
     *
     * @access  public
     * @param  string  the title
     * @param  string  the separator
     * @return  string
     */
    function url_title($str$separator '-')
    {
      
    $trans = array(
                
    '\s+'                     => $separator,
                
    '[^a-z0-9'.$separator.']'  => '',
                
    $separator.'+'            => $separator,
                
    $separator.'$'            => '',
                
    '^'.$separator            => ''
      
    );

      
    $str strtolower($str);

      foreach (
    $trans as $search => $replace)
      {
        
    $str preg_replace('#'.$search.'#'$replace$str);
      }

      return 
    $str;

    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed


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
  •