mat*_*224 1 php regex str-replace
嗨,我需要在span标签中包装字符串的最后一个字母,我怎么能在PHP中执行此操作?
例如:
$string = 'Get on the roller coaster';
Run Code Online (Sandbox Code Playgroud)
应输出:
'Get on the roller coaste<span>r</span>'
Run Code Online (Sandbox Code Playgroud)
找
(.)$
Run Code Online (Sandbox Code Playgroud)
用...来代替
<span>\1</span>
Run Code Online (Sandbox Code Playgroud)
这里演示:http://regex101.com/r/bY8kX0
在php中像这样:
<?php
$string = 'Get on the roller coaster';
echo preg_replace('/(.)$/', '<span>\1</span>', $string);
Run Code Online (Sandbox Code Playgroud)