mjs87 Posted November 1, 2011 Share Posted November 1, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/ Share on other sites More sharing options...
joe92 Posted November 1, 2011 Share Posted November 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/#findComment-1283930 Share on other sites More sharing options...
mjs87 Posted November 1, 2011 Author Share Posted November 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/#findComment-1283934 Share on other sites More sharing options...
joe92 Posted November 1, 2011 Share Posted November 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/#findComment-1283946 Share on other sites More sharing options...
mjs87 Posted November 1, 2011 Author Share Posted November 1, 2011 That works thank you. Just for my understanding what does the / before the [ do and the /s after the ]? I’ve read the link but can’t work out what these two things are doing? Thank for your help Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/#findComment-1283950 Share on other sites More sharing options...
joe92 Posted November 1, 2011 Share Posted November 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/#findComment-1283955 Share on other sites More sharing options...
mjs87 Posted November 1, 2011 Author Share Posted November 1, 2011 Great thanks for your help... I'm starting to get a better understanding of regex Quote Link to comment https://forums.phpfreaks.com/topic/250226-help-with-preg_match-for-detecting-and-characters/#findComment-1283957 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.