PHP file_get_contents - Replace all URLs in all <a href=“”> links

喜夏-厌秋 提交于 2019-12-02 04:23:25

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]

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>

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);
}

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);
Papa De Beau

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;

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