Reputation: 4220
I have a variable I want to use in a preg_match combined with some regex:
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-/d.*/", $string)) {
echo "matched";
}
In my pattern I am trying to match using cheese, followed by a - and 1 digit, followed by anything else.
Upvotes: 2
Views: 7375
Reputation: 1114
/d
to \d
.*
/
or *
or ...
)) this may cause problem on your match.Code:
<?php
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-\d/", $string))
{
echo "matched";
}
?>
Upvotes: 4
Reputation: 191829
You mistyped /
for \
:
if(preg_match("/$find-\d.*/", $string)) {
The .*
is also not really necessary since the pattern will match either way.
Upvotes: 3