语言是PHP
http://www.digitaling.com/projects/13423.html
http://baidu.com
http://www.yeelight.com/zh_CN/product/yeelight-sunflower
以上几种情况中希望能得到如下结果:
www.digitaling.com
baidu.com
www.yeelight.com
PHP中文网2017-04-10 15:15:34
为何要用正则=_=?! DEMO:http://3v4l.org/lmADo
<?php
$tests = array(
"http://www.digitaling.com/projects/13423.html",
"http://baidu.com",
"http://www.yeelight.com/zh_CN/product/yeelight-sunflower"
);
foreach($tests as $test) {
echo parse_url($test, PHP_URL_HOST), "\r\n";
}
PHP中文网2017-04-10 15:15:34
php
<?php $str = " http://www.digitaling.com/projects/13423.html http://baidu.com http://www.yeelight.com/zh_CN/product/yeelight-sunflower "; $preg = "/http:\/\/([a-zA-Z0-9.]*)/"; preg_match_all($preg, $str, $arr); var_dump($arr[1]);
最终结果:
php
array(3) { [0]=> string(18) "www.digitaling.com" [1]=> string(9) "baidu.com" [2]=> string(16) "www.yeelight.com" }
看看是不是你需要的。另外,你说的这三种情况是同在一个字符串里面呢?还是说,这三种情况随机只出现一个?