file_get_contents( - Fix relative urls

流过昼夜 提交于 2019-12-22 10:46:19

问题


I am trying to display a website to a user, having downloaded it using php. This is the script I am using:

<?php
$url = 'http://stackoverflow.com/pagecalledjohn.php';
//Download page
$site = file_get_contents($url);
//Fix relative URLs
$site = str_replace('src="','src="' . $url,$site);
$site = str_replace('url(','url(' . $url,$site);
//Display to user
echo $site;
?>

So far this script works a treat except for a few major problems with the str_replace function. The problem comes with relative urls. If we use an image on our made up pagecalledjohn.php of a cat (Something like this:

). It is a png and as I see it it can be placed on the page using 6 different urls:
1. src="//www.stackoverflow.com/cat.png"
2. src="http://www.stackoverflow.com/cat.png"
3. src="https://www.stackoverflow.com/cat.png"
4. src="somedirectory/cat.png" 

4 is not applicable in this case but added anyway!

5. src="/cat.png"
6. src="cat.png"

Is there a way, using php, I can search for src=" and replace it with the url (filename removed) of the page being downloaded, but without sticking url in there if it is options 1,2 or 3 and change procedure slightly for 4,5 and 6?


回答1:


Rather than trying to change every path reference in the source code, why don't you simply inject a <base> tag in your header to specifically indicate the base URL upon which all relative URL's should be calculated?

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

This can be achieved using your DOM manipulation tool of choice. The example below would show how to do this using DOMDocument and related classes.

$target_domain = 'http://stackoverflow.com/';
$url = $target_domain . 'pagecalledjohn.php';
//Download page
$site = file_get_contents($url);
$dom = DOMDocument::loadHTML($site);

if($dom instanceof DOMDocument === false) {
    // something went wrong in loading HTML to DOM Document
    // provide error messaging and exit
}

// find <head> tag
$head_tag_list = $dom->getElementsByTagName('head');
// there should only be one <head> tag
if($head_tag_list->length !== 1) {
    throw new Exception('Wow! The HTML is malformed without single head tag.');
}
$head_tag = $head_tag_list->item(0);

// find first child of head tag to later use in insertion
$head_has_children = $head_tag->hasChildNodes();
if($head_has_children) {
    $head_tag_first_child = $head_tag->firstChild;
}

// create new <base> tag
$base_element = $dom->createElement('base');
$base_element->setAttribute('href', $target_domain);

// insert new base tag as first child to head tag
if($head_has_children) {
    $base_node = $head_tag->insertBefore($base_element, $head_tag_first_child);
} else {
    $base_node = $head_tag->appendChild($base_element);
}

echo $dom->saveHTML();

At the very minimum, it you truly want to modify all path references in the source code, I would HIGHLY recommend doing so with DOM manipulation tools (DOMDOcument, DOMXPath, etc.) rather than regex. I think you will find it a much more stable solution.




回答2:


I don't know if I get your question completely right, if you want to deal with all text-sequences enclosed in src=" and ", the following pattern could make it:

~(\ssrc=")([^"]+)(")~

It has three capturing groups of which the second one contains the data you're interested in. The first and last are useful to change the whole match.

Now you can replace all instances with a callback function that is changing the places. I've created a simple string with all the 6 cases you've got:

$site = <<<BUFFER
1. src="//www.stackoverflow.com/cat.png"
2. src="http://www.stackoverflow.com/cat.png"
3. src="https://www.stackoverflow.com/cat.png"
4. src="somedirectory/cat.png"
5. src="/cat.png"
6. src="cat.png"
BUFFER;

Let's ignore for a moment that there are no surrounding HTML tags, you're not parsing HTML anyway I'm sure as you haven't asked for a HTML parser but for a regular expression. In the following example, the match in the middle (the URL) will be enclosed so that it's clear it matched:

So now to replace each of the links let's start lightly by just highlighting them in the string.

$pattern = '~(\ssrc=")([^"]+)(")~';

echo preg_replace_callback($pattern, function ($matches) {
    return $matches[1] . ">>>" . $matches[2] . "<<<" . $matches[3];
}, $site);

The output for the example given then is:

1. src=">>>//www.stackoverflow.com/cat.png<<<"
2. src=">>>http://www.stackoverflow.com/cat.png<<<"
3. src=">>>https://www.stackoverflow.com/cat.png<<<"
4. src=">>>somedirectory/cat.png<<<"
5. src=">>>/cat.png<<<"
6. src=">>>cat.png<<<"

As the way of replacing the string is to be changed, it can be extracted, so it is easier to change:

$callback = function($method) {
    return function ($matches) use ($method) {
        return $matches[1] . $method($matches[2]) . $matches[3];
    };
};

This function creates the replace callback based on a method of replacing you pass as parameter.

Such a replacement function could be:

$highlight = function($string) {
    return ">>>$string<<<";
};

And it's called like the following:

$pattern = '~(\ssrc=")([^"]+)(")~';
echo preg_replace_callback($pattern, $callback($highlight), $site);

The output remains the same, this was just to illustrate how the extraction worked:

1. src=">>>//www.stackoverflow.com/cat.png<<<"
2. src=">>>http://www.stackoverflow.com/cat.png<<<"
3. src=">>>https://www.stackoverflow.com/cat.png<<<"
4. src=">>>somedirectory/cat.png<<<"
5. src=">>>/cat.png<<<"
6. src=">>>cat.png<<<"

The benefit of this is that for the replacement function, you only need to deal with the URL match as single string, not with regular expression matches array for the different groups.

Now to your second half of your question: How to replace this with the specific URL handling like removing the filename. This can be done by parsing the URL itself and remove the filename (basename) from the path component. Thanks to the extraction, you can put this into a simple function:

$removeFilename = function ($url) {
    $url  = new Net_URL2($url);
    $base = basename($path = $url->getPath());
    $url->setPath(substr($path, 0, -strlen($base)));
    return $url;
};

This code makes use of Pear's Net_URL2 URL component (also available via Packagist and Github, your OS packages might have it, too). It can parse and modify URLs easily, so is nice to have for the job.

So now the replacement done with the new URL filename replacement function:

$pattern = '~(\ssrc=")([^"]+)(")~';
echo preg_replace_callback($pattern, $callback($removeFilename), $site);

And the result then is:

1. src="//www.stackoverflow.com/"
2. src="http://www.stackoverflow.com/"
3. src="https://www.stackoverflow.com/"
4. src="somedirectory/"
5. src="/"
6. src=""

Please note that this is exemplary. It shows how you can to it with regular expressions. You can however to it as well with a HTML parser. Let's make this an actual HTML fragment:

1. <img src="//www.stackoverflow.com/cat.png"/>
2. <img src="http://www.stackoverflow.com/cat.png"/>
3. <img src="https://www.stackoverflow.com/cat.png"/>
4. <img src="somedirectory/cat.png"/>
5. <img src="/cat.png"/>
6. <img src="cat.png"/>

And then process all <img> "src" attributes with the created replacement filter function:

$doc   = new DOMDocument();
$saved = libxml_use_internal_errors(true);
$doc->loadHTML($site, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_use_internal_errors($saved);

$srcs = (new DOMXPath($doc))->query('//img/@hsrc') ?: [];
foreach ($srcs as $src) {
    $src->nodeValue = $removeFilename($src->nodeValue);
}

echo $doc->saveHTML();

The result then again is:

1. <img src="//www.stackoverflow.com/cat.png">
2. <img src="http://www.stackoverflow.com/cat.png">
3. <img src="https://www.stackoverflow.com/cat.png">
4. <img src="somedirectory/cat.png">
5. <img src="/cat.png">
6. <img src="cat.png">

Just a different way of parsing has been used - the replacement still is the same. Just to offer two different ways that are also the same in part.




回答3:


I suggest doing it in more steps.

In order to not complicate the solution, let's assume that any src value is always an image (it could as well be something else, e.g. a script). Also, let's assume that there are no spaces, between equals sign and quotes (this can be fixed easily if there are). Finally, let's assume that the file name does not contain any escaped quotes (if it did, regexp would be more complicated). So you'd use the following regexp to find all image references: src="([^"]*)". (Also, this does not cover the case, where src is enclosed into single quotes. But it is easy to create a similar regexp for that.)

However, the processing logic could be done with preg_replace_callback function, instead of str_replace. You can provide a callback to this function, where each url can be processed, based on its contents.

So you could do something like this (not tested!):

$site = preg_replace_callback(
    'src="([^"]*)"',
    function ($src) {
           $url = $src[1];
           $ret = "";
           if (preg_match("^//", $url)) {
               // case 1.
               $ret = "src='" . $url . '"';
           }
           else if (preg_match("^https?://", $url)) {
               // case 2. and 3.
               $ret = "src='" . $url . '"';
           }
           else {
               // case 4., 5., 6.
               $ret = "src='http://your.site.com.com/" . $url . '"';
           }
           return $ret;
    },
    $site
);


来源:https://stackoverflow.com/questions/29495851/file-get-contents-fix-relative-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!