国产探花免费观看_亚洲丰满少妇自慰呻吟_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://m.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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁蒗| 政和县| 吴旗县| 兴海县| 锡林浩特市| 区。| 南丰县| 永定县| 西充县| 剑川县| 广水市| 根河市| 新巴尔虎左旗| 含山县| 博湖县| 晋州市| 宣化县| 周至县| 婺源县| 天峻县| 茌平县| 肥城市| 遂平县| 会宁县| 漳州市| 娄底市| 周至县| 延庆县| 宝鸡市| 九龙城区| 东乡| 上高县| 工布江达县| 曲麻莱县| 呼玛县| 汉沽区| 绥阳县| 大新县| 冕宁县| 阿鲁科尔沁旗| 嘉义市|