I am trying to match . \ or / using preg_match in PHP.
I thought this would do it but it's matching all strings.
$string = '';
$chars = '/(\.|\\|\/)/';
if (preg_match($chars, $string) != 0) {
echo 'Chars found.';
}
I am trying to match . \ or / using preg_match in PHP.
I thought this would do it but it's matching all strings.
$string = '';
$chars = '/(\.|\\|\/)/';
if (preg_match($chars, $string) != 0) {
echo 'Chars found.';
}
Argument given to preg_match()
is string. Strings are automatically escaped by PHP. For example, if you have {\\\\}
(backslash) given to the regexp engine, PHP will first parse it creating {\\}
(\\
is replaced by \
).
Next, regexp engine parses the regexp. It sees {\\}
which PHP gave to regexp engine. It sees \
as escape character, so it actually matches \
character which was escaped by \
.
In your case, it looks like /(\.|\\|\/)/
. PHP gives to regexp engine /(\.|\|\/)/
which is actually either .
or |/
(notice that |
character was escaped).
Personally, I try to avoid escaping meta-characters, especially with how regexp engine works. I usually use [.]
instead, it's more readable. Your regexp written with this would look like /([.]|\\\\|[/])/
.
It's possible to do few optimizations. While it's my personal thing, I prefer to use {}
as delimiters (yes, you can use pairs of characters). Also, your regexp matches single characters, so you could easily write it as {[.\\\\/]}
, which is very readable in my opinion (notice four slashes, it's needed because both PHP and regexp engine parse backslashes).
Also, preg_match()
returns number of matches. It will be always bigger than 0
, so you can easily consider it to be boolean and avoid writting == 0
. Instead, you can insert !
before string to make it negative. But I think you accidentally reversed condition (it matches if it doesn't match). Valid code below:
$string = '';
$chars = '{[.\\\\/]}';
if (preg_match($chars, $string)) {
echo 'Chars found.';
}