PHP uses regular expressions to convert relative paths to absolute paths

Source: Internet
Author: User
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!

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.