| | |
regular expression
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 110
Reputation:
Solved Threads: 1
Is it possible to include the character < in a regular expression? I don't seem to be able to find a reference to it anywhere and am unable to build a regex with just this one character.
•
•
•
•
Is it possible to include the character < in a regular expression? I don't seem to be able to find a reference to it anywhere and am unable to build a regex with just this one character.
eg:
PHP Syntax (Toggle Plain Text)
preg_match("/</", "<div>some text</div>", $matches);
Is there a specific way you're using <?
The most common use in PHP is:
PHP Syntax (Toggle Plain Text)
preg_match("/<(.*?)>/", "<div>some text</div>", $matches);
which matches: <div> and </div>.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Jul 2007
Posts: 110
Reputation:
Solved Threads: 1
Thanks digital-ether,
Every time I tried putting < in a regex, I found it didn't work. I'm simply trying to stop people inserting code/links in various text areas. I shall try your regex, which seems straightforward, and get back to you.
I'm wondering if I might have a problem with my keyboard.
Every time I tried putting < in a regex, I found it didn't work. I'm simply trying to stop people inserting code/links in various text areas. I shall try your regex, which seems straightforward, and get back to you.
I'm wondering if I might have a problem with my keyboard.
•
•
Join Date: Jul 2007
Posts: 110
Reputation:
Solved Threads: 1
Well digital-ether, it didn't work.
Here's my code
$comment = htmlentities($_POST['comment']);
if (preg_match("/</", $comment)) { print 'string is NOT OK!'; } else { print 'string is OK!'; }
When I use the character <, "I get string is ok".
However if I substitute , with A, then inputting A as $comment, gives me "string is not ok". ie it works.
Here's my code
$comment = htmlentities($_POST['comment']);
if (preg_match("/</", $comment)) { print 'string is NOT OK!'; } else { print 'string is OK!'; }
When I use the character <, "I get string is ok".
However if I substitute , with A, then inputting A as $comment, gives me "string is not ok". ie it works.
•
•
Join Date: Jul 2007
Posts: 110
Reputation:
Solved Threads: 1
My thanks to digital-ether, who was absolutely correct
For the benefit of others who may be experiencing regex problems, the character '<', being an html character, is changed by the use of htmlentities, which I had in my script.
When I changed the regex to "/</" instead of "/</", my if statement worked.
Regards to all
Taffd
For the benefit of others who may be experiencing regex problems, the character '<', being an html character, is changed by the use of htmlentities, which I had in my script.
When I changed the regex to "/</" instead of "/</", my if statement worked.
Regards to all
Taffd
•
•
•
•
My thanks to digital-ether, who was absolutely correct
For the benefit of others who may be experiencing regex problems, the character '<', being an html character, is changed by the use of htmlentities, which I had in my script.
When I changed the regex to "/</" instead of "/</", my if statement worked.
Regards to all
Taffd
yes, it changes to < since you used htmlentities() on it.
Normally, you want to use htmlentities() last.
So do your checks first with the raw input, then use htmlentities() as the final transformation.
Eg:
Say if you use the for a forum, then when teh user submits a post, you want to check the post for HTML tags. Then you can save the post to the database or file, etc.
When its time to display the post, you then convert it to htmlentities() before display.
This preserves the original post, allows dynamic transformations on it. (Eg: if you want to say allow HTML tags later, its still there).
Just a tip...
Using a transformation before your checks can be a security hazard also, as the transformation can cause some unpredictable results. This isn't the case for your code however...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Regular Expression (Perl)
- How to write javascript regular expression for this case? (JSP)
- regular expression (PHP)
- Regular Expression Matching New Line (Perl)
- Regular expression (Perl)
- Looking for table rows with Regular expression. (PHP)
Other Threads in the PHP Forum
- Previous Thread: How to expire pages? (prevent clicks on old ones)
- Next Thread: oracle insert query failing + php
Views: 1221 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array autosuggest beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email emptydisplayvalue error explodefunction file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery keywords limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search searchbox select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






