Solved

html tags recognition by Regular Expression

Posted on 2002-06-25
7
373 Views
Last Modified: 2012-05-04
I program a forum on my own. I have a problem when people post html tags. So I have a variable for allowable tags:
$allowtags = "b|||A|||tAble|||tr|||TD|||i|||u";
Any other tags not in the above list will be converted "<" to "&lt;" and ">" to "&gt;". Now, I need help to build a pattern for this. The pattern must work for multi-line string. And if there is a tag like "< sCripT >", it must also match (there is space/captial letters). Please help. I'm not good at Regular Expression. Thanks.
0
Comment
Question by:quanghoc
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
andreif earned 200 total points
ID: 7110129
try this code:

// $text  is users input, I'll define something here
$text = "<b> test</b> <textarea>";


$allowtags = "b|||A|||tAble|||tr|||TD|||i|||u";

// split them into array
$tags = explode('|||', $allowtags);

// RegExp itself
$text = preg_replace("/<(\s*(?!\/?(".join("|",$tags).")[>\s])[^>]*)>/i","&lt;\\1&gt;",$text);

// it's ready!
print $text;
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7110862
Bit what if:
$text = "< B> test</b> <textarea>";  
 "< B  >" with space and capital. This one should not match.
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7111068
Nope, never mind. < B > is not a valid tag. Let's me check for a couple more and the point should be granted to you qucik. Thanks.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:andreif
ID: 7111762
anyway, this < b > case is quite interesting, I'll try to sort this out :) Give me few hours
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7112283
No, as I said above, < b> is not even a valid tag. Don't worry about this. As a matter of fact, would you please explain the pattern above? Like what '\/?(' means? and what '[>\s])[^>]*' is for?
Thanks.
0
 
LVL 6

Expert Comment

by:andreif
ID: 7112892
Okay, I'll to comment it :)

we are looking for:

< - tag begin
( - everything in this brackets will appear in \\1 value
\s* - zero ore more spaces (space and tab symbols)
(?! - zero-length negative condition
\/  - character "/" (we should use slash here to show that this is just symbol without special meaning)
? - character "/" before this sign may appear 0 or 1 times
(".join("|",$tags).") - after joining this will look like (a|b|td|i) , "|" means logical OR, () need to show borders of expression
[>\s] - one of included symbols must appear. So, after keyword "a", "b", "td" must appear either ">" or \s (space) - we need this to avoid matching "img" as valid tag if "i" is allowed, etc.
) - condition's end
[^>] - everything except > (match tag's body and properties)
* - previous can appear zero or more times (non > symbols)
) - end of grouping and the end of \\1 value
> - tag end

/i in the end shows that expression is case insensitive

Hope this helps!
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7113915
Thanks a lot!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question