Home > Backend Development > PHP Tutorial > PHP regular link-get all links of content_PHP tutorial

PHP regular link-get all links of content_PHP tutorial

WBOY
Release: 2016-07-20 11:01:42
Original
1001 people have browsed it

Below are two methods of regular linking in PHP. They can obtain all links of the content and save them to an array. Of course, they can also replace all links. ​

The following are two regular link methods for PHP tutorials. They can obtain all links of the content and save them to an array. Of course, you can also replace all links.

Method 1

function get_all_url($code){
Preg_match_all('/"' ]+)["|']?s*[^>]*>([^>]+)/i',$code,$arr);
           return array('name'=>$arr[2],'url'=>$arr[1]);
}+href=["|']?([^>


Method 2

$site=substr($url,0,strpos($url,"/",8));//Site

$base=substr($url,0,strrpos($url,"/")+1);//The directory where the file is located

$fp = fopen($url, "r" );//Open url

while(!feof($fp))$contents.=fread($fp,1024);//

$pattern="|href=['"]?([^ '"]+)['" ]|u";

preg_match_all($pattern,$contents, $regarr, preg_set_order);//Match all href=

for($i=0;$i($regarr);$i++){>

if(!eregi("://",$regarr[$i][1]))//Whether it is a relative path, that is, whether there is still ://

if(substr($regarr[$i][1],0,1)=="/")//Is it the root directory of the site

echo "link".($i+1).":".$site.$regarr[$i][1]."
";//Root directory

else

echo "link".($i+1).":".$base.$regarr[$i][1]."
";//Current directory

else

echo "link".($i+1).":".$regarr[$i][1]."
";//relative path

}

fclose($fp);


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445420.htmlTechArticleThe following provide two methods about php regular links. They can - obtain all links of content and save them to an array, of course You can also replace all links. Below are two items about...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template