输出:
又如PHP函数parse_url使用方法:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">url</span><span> = </span><span class="attribute-value">'http://username:password<br>@hostname/path?arg=value#anchor'</span><span>; </span> </li> <li class="alt"><span>print_r(parse_url($url)); </span></li> <li><span>echo parse_url($url, PHP_URL_PATH); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
输出:
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li><span>( </span></li> <li class="alt"> <span>[scheme] =</span><span class="tag">></span><span> http </span> </li> <li> <span>[host] =</span><span class="tag">></span><span> hostname </span> </li> <li class="alt"> <span>[user] =</span><span class="tag">></span><span> username </span> </li> <li> <span>[pass] =</span><span class="tag">></span><span> password </span> </li> <li class="alt"> <span>[path] =</span><span class="tag">></span><span> /path </span> </li> <li> <span>[query] =</span><span class="tag">></span><span> </span><span class="attribute">arg</span><span>=</span><span class="attribute-value">value</span><span> </span> </li> <li class="alt"> <span>[fragment] =</span><span class="tag">></span><span> anchor </span> </li> <li><span>) </span></li> </ol>
可以看到,PHP函数parse_url可以很容易分解出一个URL的各个部,那如果要拿指定的部分出来的话也很容易,如
echo parse_url($url, PHP_URL_PATH);就是在第二个参数中,设定如下的参数:
PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT.