Posted By

rupakdhiman on 12/06/09


Tagged

slug regular Expression


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

xmartyxcorex
jlan


Regular expression to match a Slug


 / Published in: PHP
 

This is a regular expression to match a Slug. Slug that contains hyphens ("-").

  1. /^[a-z0-9-]+$/
  2.  
  3. To use this expression follow the code below.
  4.  
  5. //Save the expression in a variable.
  6. $correct_slug = "/^[a-z0-9-]+$/";
  7.  
  8. //some input from a user through a text field or from a server.
  9. $user_input = $_REQUEST['sometext'];
  10.  
  11. //match the two input's and save it in a variable.
  12. slug_match = preg_match($correct_slug, $user_input);
  13.  
  14. //Condition goes here.
  15. if(slug_match){
  16. echo "Correct Input";
  17. }
  18. else{
  19. echo "Incorrect Input";
  20. }

Report this snippet  

You need to login to post a comment.