我掌握了正则表达式的基本知识,并设法进行了网址检测并替换为html''标记链接。目前我有一个工作代码;目前我检测到如下链接
http://google.com
https://google.com
www.google.com
https://www.google.com
google.com
我想要实现的目标是避免将“google.com”检测为网址,因为如果它将“google.com”检测为网址,它还会检测文字/名称,例如“rameez.rami”。< / p>
<?php
$string = ' rameez.rami https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=migrate+from+mysql+to+oracle plz use http:// links instead of https:// links and http:hi all plse chec http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx http://www.google.com https://www.google.com https://www.google.com http://google.com https://google.com google.com';
$urlRegex = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
$result = preg_replace($urlRegex, '<a href="$0">$4</a>', $string);
print_r($result);
die;
?>
当前输出为:
<a href="rameez.rami">rameez.rami</a> <a href="https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=migrate+from+mysql+to+oracle">www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=migrate+from+mysql+to+oracle</a> plz use http:// links instead of https:// links and http:hi all plse chec <a href="http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx">teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx</a> <a href="http://www.google.com">www.google.com</a> <a href="https://www.google.com">www.google.com</a> <a href="https://www.google.com">www.google.com</a> <a href="http://google.com">google.com</a> <a href="https://google.com">google.com</a> <a href="google.com">google.com</a>
我的期待:
rameez.rami <a href="https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=migrate+from+mysql+to+oracle">www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=migrate+from+mysql+to+oracle</a> plz use http:// links instead of https:// links and http:hi all plse chec <a href="http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx">teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx</a> <a href="http://www.google.com">www.google.com</a> <a href="https://www.google.com">www.google.com</a> <a href="https://www.google.com">www.google.com</a> <a href="http://google.com">google.com</a> <a href="https://google.com">google.com</a> google.com
答案 0 :(得分:0)
如果你们找到了更好的答案,请发帖。
出于临时目的,我使用preg_replace_callback
函数完成了它。
$urlRegex = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
$content = ' rameez.rami https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=migrate+from+mysql+to+oracle plz use http:// links instead of https:// links and http:hi all plse chec http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx also check http://teams.lear.com/sites/Design_Guidelines/Design%20Guidelines/B_GLOBAL_ENGINEERING_DESIGN_GUIDELINES/Guideline%20Creation%20Instructions.pptx http://www.google.com https://www.google.com https://www.google.com http://google.com https://google.com google.com';
$content = preg_replace_callback(
$urlRegex,
function ($matches) use($linkClass){
$link = $matches[0];
if (!filter_var($link, FILTER_VALIDATE_URL) === false) {
return '<a class="'.$linkClass.'" href="'.$link.'" target="_blank" title="'.$link.'">'.$matches[4].'</a>';
}else{
return $link;
}
},
$content
);
print_r($content);die;