用PHP修改字符串中的链接
我试图通过添加重定向来标记字符串中的链接。数据以字符串格式从MySQL数据库中出来,如下所示:用PHP修改字符串中的链接,php,string,url,hyperlink,tagging,Php,String,Url,Hyperlink,Tagging,我试图通过添加重定向来标记字符串中的链接。数据以字符串格式从MySQL数据库中出来,如下所示: $string = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p
$string = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
Array
(
[0] => 12
[1] => 100
)
$string = str_replace(
['http://','https://'],
['http://redirect.com?link=http://', 'http://redirect.com?link=https://'],
$sourceString
);
然后,我循环遍历positions数组,并使用substr_replace在http之前添加重定向链接。但是,这只适用于一个链接,如果字符串中有多个链接,它将被覆盖。有人有什么聪明的解决办法吗
这是我的密码:
function stringInsert($str,$pos,$insertstr)
{
if (!is_array($pos))
$pos=array($pos);
$offset=-1;
foreach($pos as $p)
{
$offset++;
$str = substr($str, 0, $p+$offset) . $insertstr . substr($str, $p+$offset);
}
return $str;
}
$string = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
$needle = "http";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($string, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
$str_to_insert = "http://redirect.com?link=";
foreach ($positions as $value) {
$finalstring = substr_replace($string, $str_to_insert, $value, 0);
}
函数stringInsert($str,$pos,$insertstr)
{
如果(!是_数组($pos))
$pos=阵列($pos);
$offset=-1;
foreach(pos为$p)
{
$offset++;
$str=substr($str,0,$p+$offset)。$insertstr.substr($str,$p+$offset);
}
返回$str;
}
$string=“并请在上交朋友”;
$needle=“http”;
$lastPos=0;
$positions=array();
while(($lastPos=strpos($string,$needle,$lastPos))!==false){
$positions[]=$lastPos;
$lastPos=$lastPos+strlen($needle);
}
$str_to_insert=”http://redirect.com?link=";
foreach(头寸为$value){
$finalstring=substr\u replace($string,$str\u to\u insert,$value,0);
}
最终结果应该是:
$string = "<p><a href='http://redirect.com?link=http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://redirect.com?link=http://facebook.com'>Friend on Facebook</a></p>";
$string=“请与朋友在一起””;
试试这个
str_replace("href='", "href='http://redirect.com?link=", $string);
我认为更舒适的解决方案可能是使用str_replace() 这样做:
$string = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
Array
(
[0] => 12
[1] => 100
)
$string = str_replace(
['http://','https://'],
['http://redirect.com?link=http://', 'http://redirect.com?link=https://'],
$sourceString
);
相反,让我们使用DOMDocument解析器,它允许我们对HTML字符串使用规范化方法
$doc = new DOMDocument();
$doc->loadHTML($string);
现在,我们可以在所有锚元素上循环,并进行必要的修改:
foreach( $doc->getElementsByTagName("a") as $anchor) {
$newLink = "http://example.com";
$anchor->setAttribute('href', $newLink );
}
然后可以执行echo$doc->saveHTML()代码>完成后
您还可以在实际的foreach循环中执行条件比较。我选择regexp:
$str = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
$str = preg_replace('/([\'\"])(http[^\'\"]+)([\'\"])/', '$1http://redirect.com?link=$2$3', $str);
echo htmlspecialchars($str);
$str=“请在上交朋友”;
$str=preg\u replace('/([\'\'\“])(http[^\'\'\“]+)([\'\'\“])/,'$1http://redirect.com?link=$2$3',$str);
echo htmlspecialchars($str);
我得到的输出是:,请朋友上
请记住,最后一行仅用于显示目的。使用转义html是为了让您看到结果。对于实际工作的链接,您不需要调用htmlspecialchars
。
HTML
<p id="addRedirect"><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>
使用str_replace替换所有这是我最初的想法。