PHP正则表达式.将[text](url)转换为< a href ="url""> text</a> [英] PHP regex. Convert [text](url) to <a href="url">text</a>

查看:52
本文介绍了PHP正则表达式.将[text](url)转换为< a href ="url""> text</a>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,以将看起来像[text](url)的字符串中的所有实例转换为这样的可点击链接,例如 text ?

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)转换为&lt; a href ="url""&gt; text&lt;/a&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆