Only the characters listed on this page need to be escaped in PHP regex matching/replacing.
While < and > can act as delimiter, it doesn't need to be escaped in the given example because you already have /(slash) acting as a delimiter.
Referring to the link in question
Answer from hjpotter92 on Stack OverflowThe
preg_quote()function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.
php regex escaping special characters - Stack Overflow
php - preg_match special characters - Stack Overflow
Regex for Special Characters - PHP - SitePoint Forums | Web Development & Design Community
string - PHP preg_match: escaping { and } - Stack Overflow
Only the characters listed on this page need to be escaped in PHP regex matching/replacing.
While < and > can act as delimiter, it doesn't need to be escaped in the given example because you already have /(slash) acting as a delimiter.
Referring to the link in question
The
preg_quote()function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.
< and > aren't meta characters is most contexts.
However they are used as such for:
- named capture groups
(?P<name>) - lookbehind assertions
(?<=...)
So that's why preg_quote plays it safe and escapes them. It's arguably redundant, since escaping ( and ? would be sufficient. But it doesn't hurt either.
[\W]+ will match any non-word character.
but to match only the characters from the question, use this:
$string="sadw$"
if(preg_match("/[\[^\'£$%^&*()}{@:\'#~?><>,;@\|\\\-=\-_+\-¬\`\]]/", $string)){
//this string contain atleast one of these [^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`] characters
}
Use preg_match. This function takes in a regular expression (pattern) and the subject string and returns 1 if match occurred, 0 if no match, or false if an error occurred.
$input = 'foo';
$pattern = '/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';
if (preg_match($pattern, $input)){
// one or more matches occurred, i.e. a special character exists in $input
}
You may also specify flags and offset for the Perform a Regular Expression Match function. See the documentation link above.
In many regex implementations, the following rules apply:
Meta characters inside a character class are:
^(negation)-(range)](end of the class)\(escape char)
So these should all be escaped. There are some corner cases though:
-needs no escaping if placed at the very start, or end of the class ([abc-]or[-abc]). In quite a few regex implementations, it also needs no escaping when placed directly after a range ([a-c-abc]) or short-hand character class ([\w-abc]). This is what you observed^needs no escaping when it's not at the start of the class:[^a]means any char excepta, and[a^]matches eitheraor^, which equals:[\^a]]needs no escaping if it's the only character in the class:[]]matches the char]
[\w.-]
- the
.usually means any character but between[]has no special meaning -between[]indicates a range unless if it's escaped or either first or last character between[]