问题描述
我需要一些帮助,以将看起来像[text](url)的字符串中的所有实例转换为这样的可点击链接,例如?
I need some help converting all instances in a string that looks like this [text](url) into a clickable link like this <a href=“url“>text</a>?
推荐答案
您可以使用此常规展开式\[(.*?)\]\s*\((.*?)\)
并用<a href="$2">$1</a>
代替
You can use this regular exprestion \[(.*?)\]\s*\((.*?)\)
and replace with <a href="$2">$1</a>
like so
preg_replace('/\[(.*?)\]\s*\((.*?)\)/', '<a href="$2">$1</a>', '[text](url)');
\[(.*?)\]
在[]之间选择任何内容并将其存储在第一个捕获组中\((.*?)\)
在()之间选择任何内容并将其存储在第一个捕获组中
\[(.*?)\]
select anything between [] and store it in first capture group\((.*?)\)
select anything between () and store it in first capture group
$1
使用第一个捕获组的内容
$1
use content of first capture group
preg_replace ( $pattern , $replacement , $subject )
看看
这篇关于PHP正则表达式.将[text](url)转换为< a href ="url""> text</a>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!