markdown 文本中的URL成为超链接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 文本中的URL成为超链接相关的知识,希望对你有一定的参考价值。

$plainTextString = 'Link example with slash-escaped https - https:\/\/www.keskkonnaamet.ee\/sites\/default\/files\/lo_maahoolduse_sp_2016_uus_7.docx <br>Link example with nonescaped http -  http://www.tere.eu <br>Link example without protocol - www.tere.eu';

$regexURL = '@((http(s):\/\/)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';

//Turn plain text urls into links
$hyperlinkedString = preg_replace($regexURL, '<a href="//$0">$1</a>', stripslashes($plainTextString));

//Remove http, https protocols
$hyperlinkedString = preg_replace('/(http(s)?:\/\/)*/','',$hyperlinkedString);

echo 'Initial string:<br>'.$plainTextString.'<br><br>Hyperlinked:<br>'.$hyperlinkedString;

Initial string:  
Link example with slash-escaped https - https:\/\/www.keskkonnaamet.ee\/sites\/default\/files\/lo_maahoolduse_sp_2016_uus_7.docx   
Link example with nonescaped http - http:&#47;/www.tere.eu   
Link example without protocol - www.tere.eu  
  
Hyperlinked:   
<small>(URL hovers are identical to textual links - except in cacher ofc)</small>   
Link example with slash-escaped https - [www.keskkonnaamet.ee/sites/default/files/lo_maahoolduse_sp_2016_uus_7.docx](www.keskkonnaamet.ee/sites/default/files/lo_maahoolduse_sp_2016_uus_7.docx)  
Link example with nonescaped http - www.tere.eu   
Link example without protocol - www.tere.eu  

以上是关于markdown 文本中的URL成为超链接的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch 阻止对 Markdown 超链接的索引

使 SlickGrid 中的列成为超链接

如何将字符串中的 URL 呈现为可点击的超链接?

如何在颤动的降价中添加超链接到文本

Markdown高级语法

CTaskDialog 页脚中的超链接