This article mainly introduces how PHP converts relative paths to absolute paths using regular expressions. This article provides detailed sample code, which can be integrated into a method and called as needed, very good. Let's take a look.
Preface
Everyone should have some experience. in many cases, when making web crawlers, we need to process the hyperlinks searched by crawlers and change them to absolute paths, therefore, this article has written a regular expression to process the searched link. I will not talk about it below. let's take a look at the detailed introduction.
We may search for the following links:
Index.html/target = "_ blank" target = "_ blank"/alt = "HYPERLINK" target = "_ blank" title = "HYPERLINK"/alt = "HYPERLINK"
/
/Index.html? Id = 1? Id = 2
// Index.html // www.mafutian.net
#
##
1. jpg 1.jpeg 1.gif 1.png 1.txt
Index.html./index.html .../...
Javascript: void (0) a: B/a # a: B mailto: 'mafutian @ 126.com '/tencent: // message /? Uin = 335134463.
.../A/B/.../a./B./B
.. /C .. /.. /d .. //.. /B/c /.. /d. /.. /e #. /.. /. /f #
: 8081/index.html: 80/index.html #: 8081/index.html #: 8082/index.html
Set the first step to absolute path:
http:// ... / ../ ../
Then this article describes how to remove the implementation code of './', './', '/.' in the absolute path:
Function url_to_absolute ($ relative) {$ absolute = ''; // remove all './' $ absolute = preg_replace ('/(?
= 1); // remove the last '/..' $ absolute = preg_replace ('/(?
For more articles about how to convert relative paths to absolute paths using regular expressions in PHP, refer to PHP!