求一例PHP正则表达式的写法
黄舟
黄舟 2017-04-10 15:13:34
0
2
670

语言是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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
Ty80

为何要用正则=_=?! 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]);

最终结果:

phparray(3) { [0]=> string(18) "www.digitaling.com" [1]=> string(9) "baidu.com" [2]=> string(16) "www.yeelight.com" }

看看是不是你需要的。另外,你说的这三种情况是同在一个字符串里面呢?还是说,这三种情况随机只出现一个?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template