Php preg_用数字替换空格

Php preg_用数字替换空格,php,preg-replace,Php,Preg Replace,如何用以下不同的数字替换文本中的所有空格 作为 将来 使用preg\u replace\u回调函数的解决方案: $text = '{"id":" "},{"id":" "},{"id":" "},'; $count = 0; $text = preg_replace_callback('/" "(?=})/', function ($m) use(&$count){ return ++$count; }, $text); print_r($text); 输出: {"id":1

如何用以下不同的数字替换文本中的所有空格

作为

将来


使用
preg\u replace\u回调
函数的解决方案:

$text = '{"id":" "},{"id":" "},{"id":" "},';
$count = 0;
$text = preg_replace_callback('/" "(?=})/', function ($m) use(&$count){
    return ++$count;
}, $text);

print_r($text);
输出:

{"id":1},{"id":2},{"id":3},

如果确实需要用双引号括起数字,请使用以下内容替换回调的返回表达式:

    return '"' . ++$count . '"';

输入字符串是JSON吗?是的,就像示例中的JSON一样为什么要使用正则表达式?对它进行解码和迭代。非常感谢,你救了我一天:)你能检查一下这个问题吗
{"id":1},{"id":2},{"id":3},
    return '"' . ++$count . '"';