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.
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.
preg_quote should help you, but as @Tomalak said -- why don't you want to use str_replace or something simple (not regexps)?
I use something like this:
function escape_regexp($regexp)
{
$regex_chars = '^.[]$()|*+?{}' . "\\";
$regexp = addcslashes($regexp, $regex_chars);
return $regexp;
}
and then e.g. call:
preg_replace('/something' . escape_regexp($var1) .
'something else/', '', $string)
I didn't use preg_quote() because I wanted function for general regexp escaping, not just for PHP, but also for mysql. I wanted a different character set. I didn't want charactes < and > to be escaped - I didn't find reason for escaping them.
[\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.
You can just extract your $link string using sscanfDocs:
$source = "javascript:window.open('http://www.google.com')";
sscanf($source, "javascript:window.open('%[^']", $link);
echo $link;
(Demo) The benefit is that the syntax is easier to understand than with regular expressions and you can assign values to variables directly.
In case you want to use regular expressions, you need to quote special characters (preg_quoteDocs) before you create your pattern. This needs more work, as you must build the regex pattern prior running it:
# bare pattern, placeholder for matching group:
$pattern = "javascript:window.open('%s')";
# quote the pattern, you use ' as delimiter, it needs to be quoted
$pattern = preg_quote($pattern, "'");
# build full regex with delimiters, modifiers and inserting your match group
$pattern = sprintf("'$pattern'is", '(.*?)');
# run it
preg_match($pattern, $source, $export);
Demo
This will result in the following pattern:
'javascript\:window\.open\(\'(.*?)\'\)'is
Or as a valid PHP string:
$pattern = '\'javascript\\:window\\.open\\(\\\'(.*?)\\\'\\)\'is';
or your example:
preg_match('\'javascript\\:window\\.open\\(\\\'(.*?)\\\'\\)\'is', $source, $export);
You can always escape characters with the backslash (\). In your case:
preg_match("'javascript:window.open\(\'(.*?)\'\)'si", $source, $export);
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[]