【发布时间】:2023-03-27 04:03:01
【问题描述】:
我需要删除一个字符串的子字符串,但前提是它位于字符串的末尾。
例如,删除以下字符串末尾的“字符串”:
"this is a test string" -> "this is a test "
"this string is a test string" - > "this string is a test "
"this string is a test" -> "this string is a test"
有什么想法吗?可能是某种 preg_replace,但如何?
【问题讨论】:
-
您可能会发现
s($str)->replaceSuffix('string')很有帮助,如在 this standalone library 中找到的。
标签: php string replace preg-replace