PHP Generate URL From Text


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

URL: http://www.addedbytes.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.