语言是PHP
http://www.digitaling.com/projects/13423.htmlhttp://baidu.comhttp://www.yeelight.com/zh_CN/product/yeelight-sunflower
以上几种情况中希望能得到如下结果:
www.digitaling.com baidu.com www.yeelight.com
人生最曼妙的风景,竟是内心的淡定与从容!
为何要用正则=_=?! 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<?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
<?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]);
最终结果:
phparray(3) { [0]=> string(18) "www.digitaling.com" [1]=> string(9) "baidu.com" [2]=> string(16) "www.yeelight.com" }
array(3) { [0]=> string(18) "www.digitaling.com" [1]=> string(9) "baidu.com" [2]=> string(16) "www.yeelight.com" }
看看是不是你需要的。另外,你说的这三种情况是同在一个字符串里面呢?还是说,这三种情况随机只出现一个?
为何要用正则=_=?! DEMO:http://3v4l.org/lmADo
最终结果:
看看是不是你需要的。另外,你说的这三种情况是同在一个字符串里面呢?还是说,这三种情况随机只出现一个?