The URL completion function (FormatUrl) for collecting PHP articles ). The URL completion function is also called FormatUrl. This function is used to develop collection programs. when collecting articles, you will often encounter the path on the page as "a required function for data collection. the URL completion function can also be called FormatUrl.
This function is used to develop the collection program, when collecting articles, you will often encounter a problem where the path on the page is "relative path" or "absolute root path" is not "absolute full path", and the URL cannot be collected.
Therefore, this function needs to format the code and format all the hyperlinks so that you can directly collect the correct URL.
Path knowledge popularization
Relative path: "../" "./" or none of the above
Absolute root path:/path/xxx.html
Absolute path: http://www.xxx.com/path/xxx.html
Instance used:
The code is as follows:
$ Surl = "http://www.jb51.net /";
$ Gethtm = 'homepage solution ';
Echo formaturl ($ gethtm, $ surl );
?>
Output: home page solution
--------- Demo instance ------------
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.
The code is as follows:
Function formaturl ($ l1, $ l2 ){
If (preg_match_all ("/(] + src = \" ([^ \ "] +) \" [^>] *>) | (] + href = \ "([^ \"] +) \ "[^>] *>) | (] + src = '([^'] +) '[^>] *>) | (] + 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 );
// Determine the type
If (preg_match ("/^ (http | https | ftp) :( \// |\\\\) ([\ w \/\\\+ \-~ '@: %]) + \.) + ([\ W \/\. \ = \? \ + \-~ '@\':! % #] | (&) | &) +/I ", $ I1) {return $ l1;} // skip the url type starting with http
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,) = "mailto:" | strtolower (substr ($ I1,) = "javascript :"){
Return $ l1;
} Else {
$ I1 = $ l3. $ path. "/". $ I1;
}
Return str_replace ($ I2, "\" $ I1 \ "", $ l1 );
}
?>
The following link is used to learn PHP regular expressions. Leave a link here to prevent loss...
Bytes. This function is used to develop the collection program. when collecting articles, the path on the page is often "phase...