Regular expression to match an HTML Tag

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

This is an regular expression to match an HTML Tag from a given input.

Copy this code and paste it in your HTML
  1. /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
  3. To use this expression follow the code below.
  5. //Save the expression in a variable.
  6. $correct_htmltag_format = "/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/";
  8. //some input from a user through a text field or from a server.
  9. $user_input = $_REQUEST['html_tag'];
  11. //match the two input's and save it in a variable.
  12. tag_match = preg_match($correct_htmltag_format, $user_input);
  14. //Condition goes here.
  15. if(tag_match){
  16. echo "Correct HTML Tag";
  17. }
  18. else{
  19. echo "Incorrect HTML Tag";
  20. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.