public function url

0

I'm building my first website (I'm very new to coding).

In the form of creating user accounts on my website, it asks users for the url's of their social profiles, but it only works if they write the part that goes after of http//:www. rather than simply copy and paste the url from the browser's address bar.

This is the code that has the file .tpl ;

public function isValidUrl($url) {
    return preg_match('|^[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
    //return (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
}

public function addScheme($url) {
    $scheme = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
              || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';

    return parse_url($url, PHP_URL_SCHEME) === null ? $scheme . $url : $url;
}

Could you please tell me how to modify the previous code to simplify the users and just copy and paste the url as it appears in the browser bar?

Thanks in advance.

    
asked by k mndz 04.07.2018 в 18:31
source

1 answer

0

For this you would have to change the regular expression, I hope that it can serve you (I tried it and I think it meets what you need):

public function isValidUrl($url) {
    return preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url);
}
    
answered by 16.07.2018 / 22:29
source