Posted By

DaveChild on 10/15/08


Tagged

url php rewrite


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

karindalziel


PHP Generate URL From Text


 / Published in: PHP
 

URL: http://www.addedbytes.com

Function replaces all non-alpha-numeric chars and cleans up a text string. Should output a usable URL for almost all text.

  1. function generate_url_from_text($strText) {
  2. $strText = preg_replace('/[^A-Za-z0-9-]/', ' ', $strText);
  3. $strText = preg_replace('/ +/', ' ', $strText);
  4. $strText = trim($strText);
  5. $strText = str_replace(' ', '-', $strText);
  6. $strText = preg_replace('/-+/', '-', $strText);
  7. $strText = strtolower($strText);
  8. return $strText;
  9. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: neoteknic on August 21, 2011

Nice function for url RW!

This is the optimized version :

function generateurlfromtext($t) { return strtolower(pregreplace('/-+/','-',strreplace(' ','-',trim(pregreplace('/ +/',' ',preg_replace('/[^A-Za-z0-9-]/',' ',$t)))))); }

You need to login to post a comment.