SOLVED:回答如下
如何:
- 使用 file_get_contents 从拉取的文件中获取所有 url。该文件可以是动态的并且有多个 URL- 用自定义的新 URL 替换所有 url,并将现有的当前 url 添加到末尾作为变量
Example:
更改链接ABC.com to www.MyWebsite.com/?link=www.ABC.com
文件名:myHTML.html将使用 file_get_contents 提取的 HTML 电子邮件
<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>
需要输出到以下代码:
<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 如果这对你有帮助:)
此代码将从给定字符串中提取所有 HTTP url 并将它们放入数组中,以便您可以对数组中的链接执行任何操作:
<?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";
?>
现在,您已将 $string 变量中给出的字符串中的所有 URL 放入 $URLs 数组中。您可以 print_r URLs 数组以查看其内容或使用 for 循环遍历它(如我的示例所示)。
现在,如果您想替换字符串中的所有 URL,您可以执行以下操作:
for ($i=0;$i<count($URLs);$i++)
$string = str_replace($URLs[$i], "http://www.mysite.com?newurl=".$URLs[$i], $string);
例如,它将所有 URL 字符串替换为 http://www.mysite.com?newurl=[ACTUAL URL]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)