What is ~ in regular expression?

5

I'm with the code:

function addhttp($url){

    if(!preg_match('~^(http)s?://~i', $url)){

        $url = 'http://'.$url;
    }

    return $url;

}

It will add HTTP if the URL you entered does not contain it, but what I'm in doubt is the ~ issue you have at the beginning and then at the end ~ i . I would like to know what this is for.

    
asked by anonymous 20.02.2015 / 18:04

1 answer

3

PCRE (Perl Compatible Regular Expressions) regular expressions require delimiters that are a pair of non-alphanumeric characters where one is at the beginning and another at the end. It is also common to use the / bar as a delimiter if your regex needs to capture some you need to escape it.

 '~^(http)s?://~i    <---- modificador PCRE
--^           ^----
inicio         fim

The letter i or any other after demilitarization is one of PCRE modifiers , i means that the expression will be parsed as case insensitive, ie it does not differentiate lowercase capitals that would be the equivalent of [a-zA-Z] .

    
20.02.2015 / 18:04