更新时间:2022-01-07 21:46:30
preg_replace('/([0-9])-([0-9])/', '$1.$2', $string);
应该可以解决问题:)
更多解释:
通过在正则表达式中使用 (
和 )
,您可以创建一个组.该组可用于替换.$1
被第一个匹配的组替换,$2
被第二个匹配的组替换,依此类推.
By using (
and )
in a regular expression, you create a group. That group can be used in the replacement. $1
get replaced with the first matched group, $2
gets replaced with the second matched group and so on.
这意味着,如果您(仅作为示例)将 '$1.$2'
更改为 '$2.$1'
,该操作将交换两个数字.在您的情况下,这不是有用的行为,但也许它可以帮助您更好地理解原理.
That means that if you would (just for example) change '$1.$2'
to '$2.$1'
, the operation would swap the two numbers.
That isn't useful behavior in your case, but perhaps it helps you understand the principle better.