preg_match
字串比对剖析。语法: int preg_match(string pattern, string subject, array [matches]);传回值: 整数/阵列函式种类: 资料处理
 
内容说明 本函式以 pattern 的规则来剖析比对字串 subject。比对结果传回的值放在阵列参数 matches 之中,matches[0] 内容就是原字串 subject、matches[1] 为第一个合乎规则的字串、matches[2] 就是第二个合乎规则的字串,余类推。若省略参数 matches,则只是单纯地比对,找到则传回值为 true。

解决方案 »

  1.   

    有好多错误!
    其实就是:
    <?php
    $url="http://www.php.net/index.html";
    $ar = parse_url($url);
    foreach($ar as $k=>$v)
      echo "$k = $v<br>";
    ?>
      

  2.   

    这样才好用<?php
    $url="http://www.php.net/index.html";//URL
    $pattern1='/^(http:\/\/)?([^\/]+)/i';//get host name from URL
    $pattern2='/[^\.\/]+\.[^\.\/]+$/';//get last two segments of host name
    preg_match($pattern1,$url,$matches);
    echo count($matches)."<br>";
    echo $matches[0]."<br>";
    echo $matches[1]."<br>";
    echo $matches[2]."<br>";
    $host=$matches[2];
    preg_match($pattern2,$host,$matches);
    echo "域名是: ".$matches[0]."\n";
    ?>