Home > Backend Development > PHP Problem > How to convert relative path to absolute path in php

How to convert relative path to absolute path in php

王林
Release: 2023-03-02 09:04:02
Original
3724 people have browsed it

php method to convert a relative path to an absolute path: This can be achieved through the preg_replace() function. The preg_replace() function can perform a regular expression search and replace. If the search target is a string array, this function returns an array.

How to convert relative path to absolute path in php

#We can use the preg_replace() function to convert a relative path to an absolute path.

(Recommended learning: php tutorial)

Function introduction

preg_replace() function performs a regular expression search and replacement.

Function syntax

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
Copy after login

Search for the part matching pattern in subject and replace it with replacement.

Parameter description:

  • $pattern: The pattern to be searched, which can be a string or a string array.

  • $replacement: String or array of strings used for replacement.

  • $subject: The target string or string array to be searched and replaced.

  • $limit: Optional, the maximum number of substitutions for each subject string per pattern. The default is -1 (no limit).

  • $count: Optional, the number of times the replacement is performed.

Return value

If subject is an array, preg_replace() returns an array, otherwise it returns a string.

If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.

Code implementation:

//相对路径转化成绝对路径
<?
function relative_to_absolute($content, $feed_url) { 
preg_match(&#39;/(http|https|ftp):///&#39;, $feed_url, $protocol); 
$server_url = preg_replace("/(http|https|ftp|news):///", "", $feed_url);
//开源OSPhP.COM.CN
$server_url = preg_replace("//.*/", "", $server_url); 
    if ($server_url == &#39;&#39;) { 
        return $content; 
    } 
    if (isset($protocol[0])) {
//开源代码OSPhP.COm.CN
$new_content = preg_replace(&#39;/href="//&#39;, &#39;href="&#39;.$protocol[0].$server_url.&#39;/&#39;, $content); 
$new_content = preg_replace(&#39;/src="//&#39;, &#39;src="&#39;.$protocol[0].$server_url.&#39;/&#39;, $new_content);  //开源OSPhP.COM.CN
    } else { 
$new_content = $content; 
    } 
    return $new_content; 
} 
?>
Copy after login

The above is the detailed content of How to convert relative path to absolute path in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template