>  기사  >  백엔드 개발  >  运用PHP函数parse_url分析信息抓取_PHP教程

运用PHP函数parse_url分析信息抓取_PHP教程

WBOY
WBOY원래의
2016-07-15 13:34:56825검색

  1. $url = "http://www.electrictoolbox.
    com/php-extract-domain-from-full-url/"
    ;  
  2. $parts = parse_url($url); 

输出:

  1. Array  
  2. (  
  3. [scheme] => http  
  4. [host] => www.electrictoolbox.com  
  5. [path] => /php-extract-
    domain-from-full-url/  
  6. )  


又如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.


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445957.htmlTechArticle$ url = http://www.electrictoolbox. com/php-extract-domain-from-full-url/ ; $ parts = parse_url ($url); 输出: Array ( [scheme]= http [host]= www.electrictoolbox.com [path]= /php-...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.