PHP的file_get_contents - 更换所有与LT的所有URL; A HREF =&QUOT;&QUOT;&GT;链接 [英] PHP file_get_contents - Replace all URLs in all <a href=""> links
问题描述
解决:回答以下
HOW TO:
- 获取所有的URL从一个文件中使用的file_get_contents拉。 文件动态可以和有多个网址
- 更换自定义一个新的URL的所有URL并在末尾加上现有的当前的URL作为一个变量
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
例如:
更改链接的 www.ABC.com 为 www.MyWebsite.com/?link=www.ABC.com
的文件名:myHTML.html
该HTML电子邮件将使用的file_get_contents被拉
FILE NAME: myHTML.html The HTML email that will be pulled using file_get_contents
<body>
<p> </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>
需要输出以下code:
Need to OUTPUT to the following Code:
<body>
<p> </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>
回答我下面的工作!
附: +1如果帮你:)
Answer that worked for me below! P.S. +1 If this helped you :)
推荐答案
PHP的code,工程
The PHP Code that works
PHP code调用该文件,并替换链接
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;
?>
这篇关于PHP的file_get_contents - 更换所有与LT的所有URL; A HREF =&QUOT;&QUOT;&GT;链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!