如果 www 之前不存在,则需要添加 http
Need to have http added if not present before www
我的函数从我们的字符串 $FBdescription 中获取所有 facebook 描述文本。这个 preg_replace 做我需要的一切,除了当它在描述中找到像 www.myurl.com 的 url 时,它把它放在 href= 中,因为它不包含开头的 http://当您单击 link 时,它当然会引起问题。如果不存在,如何调整以附加 http://。
function fts_facebook_tag_filter($FBdescription)
//Converts URLs to Links in our Description Text
$FBdescription = preg_replace('@(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="[=10=]" target="_blank">[=10=]</a>', $FBdescription);
return $FBdescription;
}
不是最好但可行的解决方案:(如果缺少 http://
,仅在 href 属性内)
$FBdescription = 'BZRK Records and BZRK Black label Artists playing here More info : www.bzrk.agency https://www.facebook.com/daphne.merks/posts/988562257858538';
//Converts URLs to Links
$FBdescription = preg_replace('@(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="[=10=]" target="_blank">[=10=]</a>', $FBdescription);
$splitano = explode("www", $FBdescription);
$count = count($splitano);
$returnValue = "";
for($i=0; $i<$count; $i++) {
if (substr($splitano[$i], -6, 5) == "href=") {
$returnValue .= $splitano[$i] . "http://www";
}
else if($i < $count - 1){
$returnValue .= $splitano[$i] . "www";
}
else {
$returnValue .= $splitano[$i];
}
}
echo $returnValue;
我的函数从我们的字符串 $FBdescription 中获取所有 facebook 描述文本。这个 preg_replace 做我需要的一切,除了当它在描述中找到像 www.myurl.com 的 url 时,它把它放在 href= 中,因为它不包含开头的 http://当您单击 link 时,它当然会引起问题。如果不存在,如何调整以附加 http://。
function fts_facebook_tag_filter($FBdescription)
//Converts URLs to Links in our Description Text
$FBdescription = preg_replace('@(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="[=10=]" target="_blank">[=10=]</a>', $FBdescription);
return $FBdescription;
}
不是最好但可行的解决方案:(如果缺少 http://
,仅在 href 属性内)
$FBdescription = 'BZRK Records and BZRK Black label Artists playing here More info : www.bzrk.agency https://www.facebook.com/daphne.merks/posts/988562257858538';
//Converts URLs to Links
$FBdescription = preg_replace('@(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i', '<a href="[=10=]" target="_blank">[=10=]</a>', $FBdescription);
$splitano = explode("www", $FBdescription);
$count = count($splitano);
$returnValue = "";
for($i=0; $i<$count; $i++) {
if (substr($splitano[$i], -6, 5) == "href=") {
$returnValue .= $splitano[$i] . "http://www";
}
else if($i < $count - 1){
$returnValue .= $splitano[$i] . "www";
}
else {
$returnValue .= $splitano[$i];
}
}
echo $returnValue;