PHP file_get_contents - Replace all URLs in all

2019-03-03 05:54发布

问题:

SOLVED: Answer Below

HOW TO: - Get all the urls from a file pulled using file_get_contents. The file can by dynamic and have multiple URLs - Replace all urls with custom A NEW URL and add the existing current url on the end as a Variable

Example:

Change the link www.ABC.com to www.MyWebsite.com/?link=www.ABC.com

FILE NAME: myHTML.html The HTML email that will be pulled using file_get_contents

<body>
<p>&nbsp;</p>
<p><a href="http://www.CNN.com" target="_blank">Link One</a></p>
<p><a href="http://www.ABC.com" target="_blank">Link Two</a></p>
<p><a href="http://www.foxnews.com/politics/2013/01/28/us-planning-for-new-drone-base-in-northwest-africa-officials-say/" target="_blank">Link Three</a></p>
<p><a href="ObamaMustSee.com" target="_blank">Link Four</a></p>
</body>

Need to OUTPUT to the following Code:

 <body>
<p>&nbsp;</p>
<p><a href="http://www.MyWebsite.com/?link=http://www.CNN.com" target="_blank">Link One</a></p>
<p><a href="http://www.MyWebsite.com/?link=http://www.ABC.com" target="_blank">Link Two</a></p>
<p><a href="http://www.MyWebsite.com/?link=http://www.foxnews.com/politics/2013/01/28/us-planning-for-new-drone-base-in-northwest-africa-officials-say/" target="_blank">Link Three</a></p>
<p><a href="http://www.MyWebsite.com/?link=ObamaMustSee.com" target="_blank">Link Four</a></p>
</body>

Answer that worked for me below! P.S. +1 If this helped you :)

回答1:

This code will extract all HTTP urls from given string and put them into array so you can do whatever you want to links from array:

<?php
$string = "Test http://www.google.com test2 http://www.something.com test3 http://abc.com";
preg_match_all('!https?://[\S]+!', $string, $match);

$URLs = array();

foreach ($match as $key => $value)
    foreach ($value as $key2 => $TheUrl)
        $URLs[] = $TheUrl;


for ($i=0;$i<count($URLs);$i++)
    echo $URLs[$i]."\r\n";

?>

Now you have all URLs from string given in $string variable into $URLs array. You can print_r the URLs array to see it's content or loop through it using a for loop (as shown in my example).

Now if you want to replace all URLs in your string, you can do something like this:

for ($i=0;$i<count($URLs);$i++)
    $string = str_replace($URLs[$i], "http://www.mysite.com?newurl=".$URLs[$i], $string);

For example it will replace all URL strings to http://www.mysite.com?newurl=[ACTUAL URL]



回答2:

It's so tiresome? Try this;

$s = preg_replace_callback('~<a\s+href="(.*?)"(.*?)>(.*?)</a>~i', function($m){
    return sprintf('<a href="http://www.MyWebsite.com/?link=%s"%s>%s</a>', urlencode($m[1]), $m[2], $m[3]);
}, $html);
echo $s;

Out;

<body>
<p>&nbsp;</p>
<p><a href="http://www.MyWebsite.com/?link=http%3A%2F%2Fwww.CNN.com" target="_blank">Link One</a></p>
<p><a href="http://www.MyWebsite.com/?link=http%3A%2F%2Fwww.ABC.com" target="_blank">Link Two</a></p>
<p><a href="http://www.MyWebsite.com/?link=http%3A%2F%2Fwww.foxnews.com%2Fpolitics%2F2013%2F01%2F28%2Fus-planning-for-new-drone-base-in-northwest-africa-officials-say%2F" target="_blank">Link Three</a></p>
<p><a href="http://www.MyWebsite.com/?link=ObamaMustSee.com" target="_blank">Link Four</a></p>
</body>


回答3:

I suggest using PHP Simple HTML DOM available at http://simplehtmldom.sourceforge.net/, as it will make this significantly easier. Then you'd simply do something like:

require 'simple_html_dom.php';
function trackLinks($filename) {
    $message = file_get_contents($filename);
    foreach($message->find('a') as $link) {
        $link->href="htto://www.myWebsite.com/?link=".$link->href;
    }
    file_put_contents($filename,$message->innertext);
}


回答4:

You could just read the file line by line, and use a regular expression to search for the URLs, replacing it with your own. Here it is how I would do it:

$src = fopen('myHTMLemail.php', 'r');
$dest = fopen('myHTMLemail_changed.php', 'w');


while(false !== ($line = fgets($src)))
{
    if(preg_match('/href./', $line))
    {   
        fwrite($dest, preg_replace('/href="([^"]*)"/', 'http://www.myWebsite.com?link=${1}', $line));
    }   
    else
    {   
        fwrite($dest, $line);
    }   
}
fclose($dest);
fclose($src);


回答5:

The PHP Code that works

PHP code that calls the file and replaces the links

<?php

$message = file_get_contents("myHTML.html");


$content = explode("\n", $message);

$URLs = array();

for($i=0;count($content)>$i;$i++)
{
     if(preg_match('/<a href=/', $content[$i]))
      {
    list($Gone,$Keep) = explode("href=\"", trim($content[$i]));
    list($Keep,$Gone) = explode("\">", $Keep);
    $message= strtr($message, array( "$Keep" => "http://www.MyWesite.com/?link=$Keep", ));
      }
}

echo $message;

?>