简体   繁体   中英

extract url from text with www - PHP

I need to examine a text to extract the urls in case it contains them. As far as I have been able to find out there is this function that does it.

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match);

The problem is that this function only extracts urls if they start with http.

How can I make it extract urls that start with www.xxx as well?

You can use this regex expression to identify URLS that use www. https and http.

$url = "www.example.com";

if (preg_match_all("^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$^", $url)) {
    echo "URL is valid";
} else {
    echo "URL is invalid";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM