This regex:
$text = preg_replace("/@([^\s]+)/", "<a href=\开发者_高级运维"\\0\">\\0</a>", $text);
.. transforms all words starting with @ into links. So it turns @joshua into:
<a href="@joshua">@joshua</a>..
but I need it to be:
<a href="joshua">@joshua</a>..
so without the @ in the address of the link. Can anyone help me out with this?
$text = preg_replace('/@(\S+)/', '<a href="$1">$0</a>', $text);
Note: [^\s] can be shortened to \S.
Note: $0 is preferred over \\0 for backreferences (as stated in the manual).
$text = preg_replace("/@([^\s]+)/", "<a href=\"\\1\">\\0</a>", $text);
If you read the preg_replace documentation, you notice that \\0 is the entire match, and \\N is the N:th match. Since you already capture the name (the ([^\s]+) part), you just need to change one of the \\0:s to \\1.
EDIT: Also from the documentation, you'll see that from PHP 4.0.4, the preferred form is not \\N, but $N. So, if you have a recent (or rather, not old) PHP version, you should change it into $0 and $1.
You need to use \\1 to get the part in parentheses; \\0 is the whole match. So all you need is
$text = preg_replace("/@([^\s]+)/", "<a href=\"\\1\">\\0</a>", $text);
$text = preg_replace("/(@)([^\s]+)/", "<a href=\"\\2\">\\1\\2</a>", $text);
This is untested, but should work.
加载中,请稍侯......
精彩评论