查找符号并将其转换为锚点


Find and convert symbol to anchors

很抱歉,如果这个问题与另一个问题重复,我找不到它:-/

嘿,伙计们,我猜这是一个简单的正则表达式问题,但帮我一下好吗?Regexp在我看来总是如此复杂:-)

在本例中:

@2 Lorem ipsum悲哀坐amet,consectetur adipiscing elit。Praesent静坐阿梅夸姆安特,坐阿梅consectetur安特。码头中的非urnaadipiscing consectetur et sed lorem。Donec venenatis vehiculaporttitor。

我想将@2转换为

<a href="#2">@2</a>

但用户可以像"@2Lorem.."一样键入,但没有空格,也可以是@38427。如何获取"@"符号和后面的数字并转换为锚点?

编辑:并且@可能在文本的中间,它可能不总是在开头。

preg_replace('/@('d+)/', '<a name="#$1">@$1</a>', $string)

示例:

php > $x = 'abc @234lorem def';
php > echo preg_replace('/@('d+)/', '<a name="#$1">@$1</a>', $x);
abc <a name="#234">@234</a>lorem def
'd - any digit
+ - one or more times

find:@([0-9]+)替换:<a href="#$1">@$1</a>

preg_replace('/@([0-9]+)/', '<a name="#$1">@$1</a>', $string);