PHP:如果一个字符串以“20“、“22“、“24“或“28“开头,我该如何删除它?

sxissh06  于 2023-01-29  发布在  PHP
关注(0)|答案(1)|浏览(103)

例如,我有以下字符串:

"20 Secured file A"
"20 Secured 20 files: A, B, ...."
"22 Deleted file A"
"22 Deleted 22 files: A, B, ...."
"24 Moved file A"
"24 Moved 24 files: A, B, ...."
"28 Corrupt file A"
"28 Corrupt 28 files: A, B, ...."
"All correct 20 files: A, B, ...."
"All correct 22 files: A, B, ...."
"All correct 24 files: A, B, ...."
"All correct 28 files: A, B, ...."

我可以用PHP中的哪个函数来搜索精确的"20"并将其剪切?我测试的所有函数都只让我搜索"20"而不是"20",所以第二种情况下剪切2x "20"而不是第一个"20"。
我只需要一个代码,可以削减前三个字母,但只有当他们开始与"20","22","24"或"28"
非常感谢你们:)

str_replace('24 ', '', $text);
7cwmlq89

7cwmlq891#

您可以使用preg_replace函数来实现以下目的:

preg_replace("/^2[0248] /", '', $string);

相关问题