Jump to content

help with preg_match for detecting < - and > characters


mjs87

Recommended Posts

Hi I'm trying to use preg_match to stop the <, - and > characters from being input in a text field. I have read that the < and > characters have to be escaped e.g. /< and /> however \< and \> are anchors in regex? If anyone can help i'd really appreciate it, thanks in advance!

Link to comment
Share on other sites

Why do you need these characters to be prevented from being put into your textfield? If it is because you don't want them ending up in your database, there is a built in function called htmlspecialchars and another called htmlentities which will make those characters and more safe for you.

 

If its for another reason, please provide the code you have already tried, i.e. the full pattern, so we can help you :)

Link to comment
Share on other sites

if(preg_match('[\>]', $this->Results->Text))
return “Error – Results contains invalid characters”;

 

Unfortunately it’s for another reason, so my only option is to use preg_match. I have tried the above which seems to detect the > character. However I don’t understand why /> needs to be enclosed in the square brackets because the square brackets mean a range? Also I’m not too sure how to go on and detect the – and < characters? Thanks

 

Link to comment
Share on other sites

The [] means character class. It can be a range of characters, i.e. [0-9] which will match a digit from 0-9, or it can be individual sets of characters, [abc] which will match a or b or c. As the dash means range, you have to escape it with \-. However, the < sign and the > sign do not need to be escaped if placed within a charater class. Heres some more info on that. That guide is gold, do read it if you get some time, but especially read that section to understand which metacharaters need to be escaped within character classes.

 

if(preg_match('/[<>\-]/s', $this->Results->Text))

 

I don't know in what enviroment you are using it in and I haven't tested it so let me know if it doesn't work.

 

Hope this helps,

Joe

Link to comment
Share on other sites

The forward slashes are delimiters. The are used to contain the bounds of the pattern. There are several types of delimiters including ~ and %. They each have different pro's and con's and I don't claim to understand the differences between them all.

 

The s after the closing delimiter is a pattern modifier. Heres a list of all the modifiers available and their meanings.

 

p.s. Mark the topic as solved ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.