


PHP article collection URL completion function (FormatUrl)_PHP tutorial
Write a function that is necessary for collection, the URL completion function, which can also be called FormatUrl.
The purpose of writing this function is to develop a collection program. When collecting articles, you will often encounter that the path in the page is "relative path" or "absolute root path" and is not "absolute full path", so the URL cannot be collected.
Therefore, this function is needed to format the code and format all hyperlinks, so that the correct URL can be collected directly.
Popularization of path knowledge
Relative path: "../" "./" or add nothing in front
Absolute root path: /path/xxx.html
Absolute full path: http://www.xxx.com/path/xxx.html
Usage example:
Output: HomepageSolution
---------Demo Example------------
Original path code: http:/ /www.newnew.cn/newnewindex.aspx
Output demo code: http://www.maifp.com/aaa/test.php
The following is the function code
function formaturl($l1,$l2){
if (preg_match_all("/(< img[^>]+src="([^"]+)"[^>]*>)|(]+href="([^"]+)"[ ^>]*>)|(
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);
//Judge type
if(preg_match("/^(http|https|ftp):(//|\\)(([w/\+-~`@:%] )+.)+([w/\.=?+-~`@':!%#]|(&)|&)+/i",$I1)){return $l1; }//Start with http The url type should be skipped
elseif($I1[0]=="/"){$I1 = $l3.$I1;}//Absolute path
elseif(substr($I1,0,3 )=="../"){//Relative path
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);
}
?>
The link below is the place to learn PHP regular expressions. Leave a link here to prevent it from being lost. . .

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
