开发者

how do that in str_replace

开发者 https://www.devze.com 2023-01-06 02:49 出处:网络
I have a guestbook, and I want to convert site address in the following [link]www.yahoo.com[/link] to <a>...

I have a guestbook, and I want to convert site address in the following [link]www.yahoo.com[/link] to <a>...

S开发者_开发技巧o how is it do that str_replace?


str_replace is not powerful enough to do this. You can use preg_replace:

$res = preg_replace('#\\[link\\](?![^:]+script:)([^:<"\\[]+:)?([^<"\\[]+)\\[/link\\]#e',
                    "'<a href=\"'.('\\1'?'\\1':'http://').'\\2\">click here</a>'",
                    $input);

Example: http://www.ideone.com/lTknX

But it's better to use a BBCode parser.


This should do that for you:

$string = "[link]www.yahoo.com[/link]";

echo preg_replace("/\[link\](.*)\[\/link\]/", "<a href='$1'>click here</a>", $string);
0

精彩评论

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

关注公众号