Posted By

rupakdhiman on 12/06/09


Tagged

html regular Expression tag


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jlan
macster


Regular expression to match an HTML Tag


 / Published in: PHP
 

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

  1. /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
  2.  
  3. To use this expression follow the code below.
  4.  
  5. //Save the expression in a variable.
  6. $correct_htmltag_format = "/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/";
  7.  
  8. //some input from a user through a text field or from a server.
  9. $user_input = $_REQUEST['html_tag'];
  10.  
  11. //match the two input's and save it in a variable.
  12. tag_match = preg_match($correct_htmltag_format, $user_input);
  13.  
  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  

Comments

RSS Icon Subscribe to comments
Posted By: tlgtrn on March 20, 2013

does not work for me with "http://tr.wikipedia.org/wiki/S"

Posted By: tlgtrn on March 20, 2013

does not work for me with: 'http://tr.wikipedia.org/wiki/S'

You need to login to post a comment.