PHP 集成友好url Generate Search Engine Friendly URLs with PHP Functions

Generating search engine friendly (SEF) URLs can dramatically improve your search engine results. There's a big difference between "/post.php?id=2382" and "/great-php-functions/". Having search engine friendly URLs also gives the user an idea of what will be on the page they are clicking on if the link text isn't adequate.

 

I've created sister PHP functions to generate search engine friendly URLs for the CMS' I create for my customers. The idea is fairly simple. I take the user-created page title and feed it to a scrubbing function to:

  • remove all punctuation
  • switch the URL to lowercase
  • remove spaces, replace with a given delimiter (in this case, a dash)
  • remove duplicate words
  • remove words that aren't helpful to SEO

The Code

Php代码  收藏代码
  1. /* takes the input, scrubs bad characters */  
  2. function generate_seo_link($input$replace = '-'$remove_words = true, $words_array = array()) {  
  3.     //make it lowercase, remove punctuation, remove multiple/leading/ending spaces  
  4.     $return = trim(ereg_replace(' +'' ', preg_replace('/[^a-zA-Z0-9\s]/'''strtolower($input))));  
  5.   
  6.     //remove words, if not helpful to seo  
  7.     //i like my defaults list in remove_words(), so I wont pass that array  
  8.     if($remove_words) { $return = remove_words($return$replace$words_array); }  
  9.   
  10.     //convert the spaces to whatever the user wants  
  11.     //usually a dash or underscore..  
  12.     //...then return the value.  
  13.     return str_replace(' '$replace$return);  
  14. }  
  15.   
  16. /* takes an input, scrubs unnecessary words */  
  17. function remove_words($input,$replace,$words_array = array(),$unique_words = true)  
  18. {  
  19.     //separate all words based on spaces  
  20.     $input_array = explode(' ',$input);  
  21.   
  22.     //create the return array  
  23.     $return = array();  
  24.   
  25.     //loops through words, remove bad words, keep good ones  
  26.     foreach($input_array as $word)  
  27.     {  
  28.         //if it's a word we should add...  
  29.         if(!in_array($word,$words_array) && ($unique_words ? !in_array($word,$return) : true))  
  30.         {  
  31.             $return[] = $word;  
  32.         }  
  33.     }  
  34.   
  35.     //return good words separated by dashes  
  36.     return implode($replace,$return);  
  37. }  

 

The Explanation

The function accepts four values:

  1. $input - string - will be SEO'd, in my case, the page title
  2. $replace - string - the word separator, in most cases a dash or underscore
  3. $remove_words - boolean - remove specific, non-helpful SEO words
  4. $words_array - array - an array of words that should be removed from every URL because they aren't helpful to SEO

Example Results

Php代码  收藏代码
  1. $bad_words = array('a','and','the','an','it','is','with','can','of','why','not');  
  2. echo generate_seo_link('Another day and a half of PHP meetings''-', true, $bad_words);  
  3. //displays :: another-day-half-php-meetings  
  4.   
  5. echo generate_seo_link('CSS again?  Why not just PHP?''-', true, $bad_words);  
  6. //displays :: css-again-just-php  
  7.   
  8. echo generate_seo_link('A penny saved is a penny earned.''-', true, $bad_words);  
  9. //displays :: penny-saved-earned  

 

Do yourself a favor -- make your dynamic pages more search engine friendly with clean URLs!

 

来源:http://davidwalsh.name/php-seo

发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览