PHP - 帮助替换字符串中的特定字符

问题描述 投票:0回答:1

我的文本行看起来像这样...

My name is George 123123123

I like cheese and crackers 123456789

每个句子可以是任意长度,但始终以空格结尾,后跟 9 位数字。

我需要句子结尾,空格连字符空格,然后是 9 位数字。

但是...你无法改变句子的长度。

我不想把它改成

My name is George - 123123123

在此示例中,应更改为

My name is Geor - 123123123

php regex replace substring
1个回答
0
投票

你可以尝试一个简单的正则表达式:

preg_replace('/.. ([0-9]{9})$/', ' - \1', 'My name is George 123123123')

产生

My name is Geor - 123123123
© www.soinside.com 2019 - 2024. All rights reserved.