开发者

PHP: Add Unique ID + Order ID for each word what starts with @

I need to add an Order ID + Unique ID for each word what starts with @.


For example I h开发者_Go百科ave a string like this:

Just @do @it and @do @it.

I want to preg_replace #(\@)+([^\s]+)#i to this:

Just <div id="1+Unique ID">@do</div> <div id="2+Unique ID">@it</div> and <div id="3+Unique ID">@do</div> <div id="4+Unique ID">@it</div>.


You can use the /e flag to preg_replace to run code for each replacement:

$string = 'Just @do @it and @do @it.';
$id = 0;

echo preg_replace('/@\w+/e', '"<div id=\"".++$id."\">\\0</div>"', $string);

Output:

Just <div id="1">@do</div> <div id="2">@it</div> and <div id="3">@do</div> <div id="4">@it</div>.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜