小编ast*_*rox的帖子

删除字符串中的所有数字,除非它们遵循PHP中的某个字符

假设我有很多字符串,其中字符没有预定位置.....

$string = '324 Example words #25 more words';
$string2 = 'Sample words 324 Example words #25 more words #26';
Run Code Online (Sandbox Code Playgroud)

我想删除php字符串中的所有数字,除非它们立即跟随'#'字符.关于在字符后删除字符串部分的帖子很多,但我想只保留跟随某个字符的数字,直到下一个空格.以上示例字符串应如下所示......

   $string = 'Example words #25 more words';
   $string2 = 'Sample words Example words #25 more words #26';
Run Code Online (Sandbox Code Playgroud)

可能吗?这可以用正则表达式完成吗?如何修改以下代码片段来完成此操作?

  $string = preg_replace('/[0-9]+/', '', $string);
Run Code Online (Sandbox Code Playgroud)

php regex string

4
推荐指数
1
解决办法
66
查看次数

标签 统计

php ×1

regex ×1

string ×1