User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,974 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,754 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 839 | Replies: 5 | Solved
Reply
Join Date: Jul 2007
Posts: 110
Reputation: Taffd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Taffd Taffd is offline Offline
Junior Poster

regular expression

  #1  
Nov 27th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2005
Posts: 707
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 43
Moderator
digital-ether's Avatar
digital-ether digital-ether is online now Online
Master Poster

Help Re: regular expression

  #2  
Nov 28th, 2007
Originally Posted by Taffd View Post
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.


Yes, there is nothing special about the < character.

eg:

preg_match("/</", "<div>some text</div>", $matches);

Is there a specific way you're using <?

The most common use in PHP is:

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!
Reply With Quote  
Join Date: Jul 2007
Posts: 110
Reputation: Taffd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Taffd Taffd is offline Offline
Junior Poster

Re: regular expression

  #3  
Nov 28th, 2007
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.
Reply With Quote  
Join Date: Jul 2007
Posts: 110
Reputation: Taffd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Taffd Taffd is offline Offline
Junior Poster

Re: regular expression

  #4  
Nov 28th, 2007
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.
Reply With Quote  
Join Date: Jul 2007
Posts: 110
Reputation: Taffd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Taffd Taffd is offline Offline
Junior Poster

Re: regular expression

  #5  
Nov 28th, 2007
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 "/&lt;/" instead of "/</", my if statement worked.

Regards to all
Taffd
Reply With Quote  
Join Date: Sep 2005
Posts: 707
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 43
Moderator
digital-ether's Avatar
digital-ether digital-ether is online now Online
Master Poster

Help Re: regular expression

  #6  
Nov 28th, 2007
Originally Posted by Taffd View Post
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 "/&lt;/" instead of "/</", my if statement worked.

Regards to all
Taffd


hi Taffd,

yes, it changes to &lt; 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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC