Regular expression to match a Slug


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

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


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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.