国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php解析字符串里所有URL地址的方法

2024-05-04 23:33:53
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php解析字符串里所有URL地址的方法,涉及php操作數組、字符串及URL的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php解析字符串里所有URL地址的方法。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. // $html = the html on the page 
  3. // $current_url = the full url that the html came from 
  4. //(only needed for $repath) 
  5. // $repath = converts ../ and / and // urls to full valid urls 
  6. function pageLinks($html$current_url = ""$repath = false){ 
  7. preg_match_all("//<a.+?href=(/"|')(?!javascript:|#)(.+?)(/"|')/i"$html$matches); 
  8. $links = array(); 
  9. if(isset($matches[2])){ 
  10. $links = $matches[2]; 
  11. if($repath && count($links) > 0 && strlen($current_url) > 0){ 
  12. $pathi = pathinfo($current_url); 
  13. $dir = $pathi["dirname"]; 
  14. $base = parse_url($current_url); 
  15. $split_path = explode("/"$dir); 
  16. $url = ""
  17. foreach($links as $k => $link){ 
  18. if(preg_match("/^/././"$link)){ 
  19. $total = substr_count($link"../"); 
  20. for($i = 0; $i < $total$i++){ 
  21. array_pop($split_path); 
  22. $url = implode("/"$split_path) . "/" . str_replace("../"""$link); 
  23. }elseif(preg_match("/^/////"$link)){ 
  24. $url = $base["scheme"] . ":" . $link
  25. }elseif(preg_match("/^//|^.///"$link)){ 
  26. $url = $base["scheme"] . "://" . $base["host"] . $link
  27. }elseif(preg_match("/^[a-zA-Z0-9]/"$link)){ 
  28. if(preg_match("/^http/"$link)){ 
  29. $url = $link
  30. }else
  31. $url = $dir . "/" . $link
  32. $links[$k] = $url
  33. return $links
  34. header("content-type: text/plain"); 
  35. $url = "http://www.survivalescaperooms.com"
  36. $html = file_get_contents($url); 
  37. // Gets links from the page: 
  38. print_r(pageLinks($html)); 
  39. // Gets links from the page and formats them to a full valid url: 
  40. print_r(pageLinks($html$url, true)); 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长岭县| 南通市| 安达市| 乐昌市| 福清市| 汾阳市| 虞城县| 丰城市| 永平县| 罗甸县| 卢氏县| 垣曲县| 昌宁县| 诸城市| 石河子市| 孝昌县| 江北区| 东乡县| 孟州市| 盖州市| 将乐县| 宜兴市| 广灵县| 武义县| 德昌县| 阿坝县| 梁河县| 安陆市| 尉氏县| 博兴县| 高安市| 甘德县| 敦煌市| 佛冈县| 宿松县| 遵义市| 广安市| 股票| 河间市| 菏泽市| 上虞市|