phpregexpreg-matchurl-validation

Escaping a comma


I have this regex

(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))

but I'm having trouble with the comma. Escaping the comma like this \, does not solve the problem. What can I do to make this regex works work??

My code:

if (preg_match("/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/", "https://google.com/picture.jpeg")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Thanks in advance.


Solution

  • You should use single apostrophes here and escape the single apostrophe:

    if (preg_match('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', "https://google.com/picture.jpeg")) {
    //             ^                                                                                                                                                                        ^                 ^
        echo "A match was found.";
    } else {
        echo "A match was not found.";
    }
    

    See IDEONE demo

    Otherwise, you would have to double the backslashes to actually represent literal backslashes. Note that escaping a comma is not necessary at all. You don't even have to escape the hyphen at the final position inside the character class [a-z0-9.-].