PHP实现文章采集URL补全函数(FormatUrl)的解决办法
内容摘要
这篇文章主要为大家详细介绍了PHP实现文章采集URL补全函数(FormatUrl)的简单示例,具有一定的参考价值,可以用来参考一下。
文章正文
这篇文章主要为大家详细介绍了PHP实现文章采集URL补全函数(FormatUrl)的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
写采集必用的函数,URL补全函数,也可叫做FormatUrl。写此函数作用就是为了开发采集程序,采集文章的时候会经常遇到页面里的路径是 “相对路径” 或者 “绝对根路径” 不是“绝对全路径”就无法收集URL。所以,就需要本功能函数进行对代码进行格式化,把所有的超链接都格式化一遍,这样就可以直接收集到正确的URL了。路径知识普及相对路径:“../” “./” 或者前面什么都不加绝对根路径:/path/xxx.html绝对全路径:http://www.xxx.com/path/xxx.html使用实例:代码如下:
1 2 3 4 5 6 7 8 | <code class = "language-php" ><?php /* php教程 www.512Pic.com */ $surl = "https://www.idcnote.com/" ; $gethtm = '<a href="/index.htm">首页</a><a href="Resolvent/index.htm">解决方案</a>' ; echo formaturl( $gethtm , $surl ); ?> </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <code class = "language-php" ><?php /* php教程 www.512Pic.com */ function formaturl( $l1 , $l2 ){ if (preg_match_all( "/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i" , $l1 , $regs )){ foreach ( $regs [0] as $num => $url ){ $l1 = str_replace ( $url ,lIIIIl( $url , $l2 ), $l1 ); } } return $l1 ; } function lIIIIl( $l1 , $l2 ){ if (preg_match( "/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i" , $l1 , $regs )){ $I2 = $regs [3];} if ( strlen ( $I2 )>0){ $I1 = str_replace ( chr (34), "" , $I2 ); $I1 = str_replace ( chr (39), "" , $I1 ); } else { return $l1 ;} $url_parsed = parse_url ( $l2 ); $scheme = $url_parsed [ "scheme" ]; if ( $scheme != "" ){ $scheme = $scheme . "://" ;} $host = $url_parsed [ "host" ]; $l3 = $scheme . $host ; if ( strlen ( $l3 )==0){ return $l1 ;} $path = dirname( $url_parsed [ "path" ]); if ( $path [0]== "\\" ){ $path = "" ;} $pos = strpos ( $I1 , "#" ); if ( $pos >0) $I1 = substr ( $I1 ,0, $pos ); //判断类型 if (preg_match( "/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i" , $I1 )){ return $l1 ; } //http开头的url类型要跳过 elseif ( $I1 [0]== "/" ){ $I1 = $l3 . $I1 ;} //绝对路径 elseif ( substr ( $I1 ,0,3)== "../" ){ //相对路径 while ( substr ( $I1 ,0,3)== "../" ){ $I1 = substr ( $I1 , strlen ( $I1 )-( strlen ( $I1 )-3), strlen ( $I1 )-3); if ( strlen ( $path )>0){ $path = dirname( $path ); } } $I1 = $l3 . $path . "/" . $I1 ; } elseif ( substr ( $I1 ,0,2)== "./" ){ $I1 = $l3 . $path . substr ( $I1 , strlen ( $I1 )-( strlen ( $I1 )-1), strlen ( $I1 )-1); } elseif ( strtolower ( substr ( $I1 ,0,7))== "mailto:" || strtolower ( substr ( $I1 ,0,11))== "javascript:" ){ return $l1 ; } else { $I1 = $l3 . $path . "/" . $I1 ; } return str_replace ( $I2 , "\"$I1\"" , $l1 ); } ?> </code> |
注:关于PHP实现文章采集URL补全函数(FormatUrl)的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释