PHP文章采集URL補(bǔ)全函數(shù)_PHP教程
推薦:PHP中全局變量global和$GLOBALS[]的區(qū)別一、舉例比較 例一: ?php $var1 =1; function test(){ unset( $GLOBALS [ 'var1' ]); } test(); echo $var1 ; ? 因?yàn)?var1被刪除了,所以什么東西都沒(méi)有打
寫(xiě)采集必用的函數(shù),URL補(bǔ)全函數(shù),也可叫做FormatUrl。
寫(xiě)此函數(shù)作用就是為了開(kāi)發(fā)采集程序,采集文章的時(shí)候會(huì)經(jīng)常遇到頁(yè)面里的路徑是 “相對(duì)路徑” 或者 “絕對(duì)根路徑” 不是“絕對(duì)全路徑”就無(wú)法收集URL。
所以,就需要本功能函數(shù)進(jìn)行對(duì)代碼進(jìn)行格式化,把所有的超鏈接都格式化一遍,這樣就可以直接收集到正確的URL了。
路徑知識(shí)普及
相對(duì)路徑:“../” “./” 或者前面什么都不加
絕對(duì)根路徑:/path/xxx.html
絕對(duì)全路徑:http://www.xxx.com/path/xxx.html
使用實(shí)例:
- <?php
- $surl="http://www.soqi.cc/";
- $gethtm = '<a href="/">首頁(yè)</a><a href="/daxue/">大學(xué)排行</a>';
- echo formaturl($gethtm,$surl);
- ?>
輸出:
- <a href=http://www.soqi.cc>首頁(yè)</a><a href=http://www.soqi.cc/daxue/>大學(xué)排行</a>
函數(shù)代碼如下:
- <?php
- 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 );
- //判斷類(lèi)型
- if (preg_match ( "/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i", $I1 )) {
- return $l1;
- } //http開(kāi)頭的url類(lèi)型要跳過(guò)
- elseif ($I1 [0] == "/") {
- $I1 = $l3 . $I1;
- } //絕對(duì)路徑
- elseif (substr ( $I1, 0, 3 ) == "../") { //相對(duì)路徑
- 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 );
- }
- ?>
分享:php輸出echo、print、print_r、printf、sprintf、var_dump比較一、echo echo() 實(shí)際上不是一個(gè)函數(shù),是php語(yǔ)句,因此您無(wú)需對(duì)其使用括號(hào)。不過(guò),如果您希望向 echo() 傳遞一個(gè)以上的參數(shù),那么使用括號(hào)會(huì)發(fā)生解析錯(cuò)誤。而且echo是返回void的,并不返回值,所以不能使用它來(lái)賦值。 例子: 1 ? php 2 $a = echo (55nav); // 錯(cuò)誤!不
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問(wèn)控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- 十天學(xué)會(huì)php之第九天
- mysql 時(shí)間轉(zhuǎn)換函數(shù)的用法
- 詳細(xì)學(xué)習(xí)PHP中對(duì)文件和目錄的操作方法
- 使用php get_headers 判斷URL是否有效的解決辦法
- 解析PHP安全編程的加密功能
- 十天學(xué)會(huì)php之第六天
- 在PHP中使用隨機(jī)數(shù)的三個(gè)步驟詳細(xì)代碼
- PHP實(shí)例:PHP批量生成縮略圖
- PHP調(diào)用MsSQL Server 2012存儲(chǔ)過(guò)程獲取多結(jié)果集(包含output參數(shù))的詳解
- PHP header()函數(shù)使用詳細(xì)(301、404等錯(cuò)誤設(shè)置)
- 相關(guān)鏈接:
- 教程說(shuō)明:
PHP教程-PHP文章采集URL補(bǔ)全函數(shù)
。