求一例PHP正则表达式的写法

WBOY
Release: 2016-06-06 20:39:50
Original
1124 people have browsed it

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

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

为何要用正则=_=?! DEMO:http://3v4l.org/lmADo

<code><?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";
}
</code></code>
Copy after login

<code>php</code><code><br><?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]);

</code></code>
Copy after login

最终结果:

<code>php</code><code>array(3) { [0]=> string(18) "www.digitaling.com" [1]=> string(9) "baidu.com" [2]=> string(16) "www.yeelight.com" }
</code>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template