开发者

How to Create Hyper Link from User Defined Tag Using Regular Expression

How can I create a hyper link from following string.

[url =http://go开发者_JAVA百科ogle.com] Google [/url] 

it should be replaced with

<a href="http://www.google.com">Google</a>

The anchor tag is structured with the url as the src attribute and the center as a caption


I am assuming you are writing bbcode. If so writing an actual parser would be faster but if you just want to do search and replace something like

preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/i', '<a href='.$1.'>'.$2.'</img>');

I cannot remember the jsfiddle like site that lets you show regexp so if someone know it please leave it in the comments.

Also if you are gonna just copy and paste my code into your bbcode seach and replace file... DONT it's vulnerable to xss injection and really not an amazing expression it just is an example.


The function that you would want to use is preg_split.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜