URL completion function (FormatUrl) _ PHP instance collected by php articles

Source: Internet
Author: User
This function is used to develop the collection program, when collecting articles, we often encounter that the paths on the page are relative paths or absolute root paths are not absolute full paths, so we cannot collect the functions required for URL writing collection. the URL completion function, it 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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.