찾다

 >  Q&A  >  본문

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

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

黄舟黄舟2793일 전683

모든 응답(2)나는 대답할 것이다

  • PHP中文网

    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";
    }
    

    회신하다
    0
  • PHP中文网

    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]);

    最终结果:

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

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

    회신하다
    0
  • 취소회신하다