how do that in str_replace
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);
精彩评论