SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating Search Engine Friendly URL based on Post Title

    Let's say you were developing a news posting script (or blog, whatever) and for each entry you have a Title and Article field. Now let's say that you want to have search engine friendly links on your site for all your news stories. You want to generate the search engine friendly URL based on the Title of the news posting. So for example you post a news story with a title of "The New Sitepoint Book is About XHTML." Your link to the article would be something like:

    domain.com/news/new-sitepoint-book-xhtml/

    How would you develop a function that would take the Title of a news posting and strip out all garbage out of it and return a search engine friendly URL? What kinds of characters or words would you strip out of the Title (like 'a' 'or' 'and' etc...)? How would you format a regular expression to strip out all non alphanumeric characters? What's the best method for doing something like this?

    Thanks.
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  2. #2
    Team SitePoint andrew.k's Avatar
    Join Date
    Nov 2006
    Location
    Melbourne, Australia
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function slug_from_title$title ) {
      
    $title strtolower($title); 
      
    $title preg_replace('/[^a-z0-9 _-]/'''$title);
      
    $title preg_replace('/\s+/''-'$title);
      
    $title preg_replace('|-+|''-'$title);
      
    $title trim($title'-');
      return 
    $title;

    That's a pretty simple implementation, which doesn't account for octets or entities, but it's should be enough to get you going.
    Andrew Krespanis
    SitePoint Code Monkey

  3. #3
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this also does the trick
    PHP Code:
    function url_string($string)
    {
        
    $string ereg_replace("[^[:space:]a-zA-Z0-9_-]"""$string);
        
    $string trim($string);
        
    $string urlencode($string);
        
    $string ereg_replace("[+]+","-",$string);
       
       return 
    $string;

    I'd be interested in seein a bit more of your code, I want to implement a similar system myself and am researching different approaches
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  4. #4
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by andrew.k View Post
    PHP Code:
    function slug_from_title$title ) {
      
    $title strtolower($title); 
      
    $title preg_replace('/[^a-z0-9 _-]/'''$title);
      
    $title preg_replace('/\s+/''-'$title);
      
    $title preg_replace('|-+|''-'$title);
      
    $title trim($title'-');
      return 
    $title;

    That's a pretty simple implementation, which doesn't account for octets or entities, but it's should be enough to get you going.

    What do you mean by octets and entities? octets = hex code and entities = html entites (&nbsp) ?
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  5. #5
    Team SitePoint andrew.k's Avatar
    Join Date
    Nov 2006
    Location
    Melbourne, Australia
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is indeed what I mean.

    Your best bet would be to have a look in WordPress, Drupal or one of the other open source PHP applications that does the same thing. What you're trying to achieve is very common, no need to re-invent the wheel
    Andrew Krespanis
    SitePoint Code Monkey


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
  •