I have the following code that checks if a domain has the pattern "Text-Text-Integer". However I get the error Unknown modifier '-' because the - inside is not escaped. I tried to add \ before - but it does not help.
<?php
$get_keyword_value = "([a-zA-Z])-([a-zA-Z])-([0-9])";
$get_current_domain_value = "domain-id-555";
if(preg_match($get_keyword_value, $get_current_domain_value)){
echo"Match";
}
else{
echo"No match";
}
?>
CodePudding user response:
Change your pattern to this:-
$get_keyword_value = "/([a-zA-Z] )-([a-zA-Z] )-([0-9])/";
(Edit:- forgot to add ending colon)
CodePudding user response:
The pattern needs to be delimited with a character, such as "/([a-zA-Z])-([a-zA-Z])-([0-9])/", "@([a-zA-Z])-([a-zA-Z])-([0-9])@", "#([a-zA-Z])-([a-zA-Z])-([0-9])#" or whatever.
This allows you to pass modifiers like "/([a-zA-Z])-([a-zA-Z])-([0-9])/i" for a case insensitive match, in example.
"([a-zA-Z])-([a-zA-Z])-([0-9])" will use the () as delimiter. Then, the engine tries to use the modifiers, and the first one is - which doesn't exist.
Side note : Your pattern won't match the input, because ([a-zA-Z]) will search for a single character, but this is another topic
