Home  »     »     »     »  Function to generate an seo friendly url - Clean url function in php

Function to generate an seo friendly url - Clean url function in php

Tuesday, April 17, 2012

Function to generate an seo friendly url from the supplied string

function clean_url($untouched_str){

    $cleaned_str = preg_replace('/\%/',' percentage',$untouched_str);  //replace '%' with 'percentage'
    $cleaned_str = preg_replace('/\@/',' at ',$cleaned_str);  //replace '@' with 'at'
    $cleaned_str = preg_replace('/\&/',' and ',$cleaned_str); //replace '&' with 'and'
    $cleaned_str = preg_replace('/\s[\s]+/','-',$cleaned_str);    // Strip off multiple spaces
    $cleaned_str = preg_replace('/[\s\W]+/','-',$cleaned_str);    // Strip off spaces and non-alpha-numeric
    $cleaned_str = preg_replace('/^[\-]+/','',$cleaned_str); // Strip off the starting hyphens
    $cleaned_str = preg_replace('/[\-]+$/','',$cleaned_str); //  Strip off the ending hyphens
    $cleaned_str = strtolower($cleaned_str); //convert to lowercase
    return $cleaned_str;
}


usage:

echo( clean_url("Test   url!@@ "));

the result will be like :

test-url



How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
Function to generate an seo friendly url - Clean url function in php.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »