我有一个具有以下结构的字符串。我要替换最后一个下划线之后的字符。
$string = '
{$9018049_text_50024080_3} : {$9018049_text_53441884_3}
{$9018049_text_50024080_4} : {$9018049_text_53441884_4}
{$9018049_text_50024080_5} : {$9018049_text_53441884_5}
';
例如,如果我将字符“ 5”替换为“ DT5”,则输出应为
$string = '
{$9018049_text_50024080_3} : {$9018049_text_53441884_3}
{$9018049_text_50024080_4} : {$9018049_text_53441884_4}
{$9018049_text_50024080_DT5} : {$9018049_text_53441884_DT5}
';
我已经尝试过str_replace,但是输出是
$string = '
{$9018049_text_DT50024080_3} : {$9018049_text_DT53441884_3}
{$9018049_text_DT50024080_4} : {$9018049_text_DT53441884_4}
{$9018049_text_DT50024080_DT5} : {$9018049_text_DT53441884_DT5}
';
这不是我想要的。任何帮助将不胜感激。
如果我了解得很好:
$string = str_replace('_5}', '_DT5}', $string);
如果您想对大括号之间的每个内容执行相同的操作,无论数字是多少:
$string = preg_replace('~_(\d+})~', '_DT$1', $string);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句