简体   繁体   中英

convert urls to domains using regular expression in php

I have a $urls array, that contain urls of some pictures or anything else:

$urls = array("http://shop.google.com/pic/android2014-1.jpg",
"http://shop.about.com/pic/buy2.gif",
"http://shop.ebay.com/pic/android2014-2.jpg",
"http://shop.somesite.com/pic/android2014-3.jpg",
"http://shop.wordpress.com/pic/android2014-6.jpg",
"http://shop.test.com/pic/android2014-4.jpg");

I need to my output to be something like this:

$domains = array("shop.google.com",
"shop.about.com",
"shop.ebay.com",
"shop.somesite.com",
"shop.wordpress.com",
"shop.test.com");

I found a solution like this example . It works well, but I want to use regular expression in my foreach loop. I think I should use preg_math and I read the php.net documentation, but I don't know how can I do this with regular expression

$domains=array();
foreach($urls as $url){
    $domain = parse_url($url, PHP_URL_HOST);
    array_push($domains,$domain);
}

You can simply print_r($domains); to print the domain names.


Using Regular expression

preg_match("/^(http:\/\/)?([^\/]+)/i", $url, $matches);
$domain = $matches[2];

Functional style:

$domains = array_map(function($url) { return parse_url($url, PHP_URL_HOST); }, $urls); 
var_dump($domains);

See working example here .

Try this:

preg_match("/(?<=http:\/\/)(.*?)(?=\/)/sm", $input_line, $output_array);

Explanation

The first part is using positive lookbehind to match anything after the "http://" (google lookbehind and lookahead " for more information).

(?<=http:\/\/)(.*?)

The Last part is using positive lookahead to match anything before the /(folder)/(filename).gif:

(?=\/)

Result: http://www.phpliveregex.com/p/3TZ


To also match https (or other protocols)

preg_match("/(?<=\w:\/\/)(.*?)(?=\/)/sm", $input_line, $output_array);

Result: http://www.phpliveregex.com/p/3U0

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