Search code examples
phppreg-match

preg_match with dot matches non-dot too


I am trying to match .com (along with word and @) with preg_match like this:

if(preg_match("/(word|.com|\@)/i", $content)){
   // dance here
}

The problem is it matches the word completely for example, too. It should only match .com. How do I fix that?


Solution

  • . is a wildcard for "any single character". Since you watch to match a literal ., you have to escape it:

    if(preg_match("/(word|\.com|\@)/i", $content)){
                          ^---