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 directoryelse
echo "link".($i+1).":".$base.$regarr[$i][1]."
";//Current directoryelse
echo "link".($i+1).":".$regarr[$i][1]."
";//relative path}
fclose($fp);