一段代码可以解释我的问题
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$patterns = array();
$patterns[0] = '/[0-9]+\s[0-9]+/';
$replacements = array();
$replacements[0] = '$0';
echo preg_replace($patterns, $replacements, $string,1);
我需要在数字之间加上一个,比如:
$string = '53-69 cm; 988-2460 g; wing 106-116 cm';
如何把 - 替换?
由于
答案 0 :(得分:4)
希望这会给你一些帮助:
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$pattern = '/(\d+)\s(\d+)/';
$replacement = '$1-$2';
echo preg_replace($pattern, $replacement, $string);
输出:
53-69 cm; 988-2460 g; wing 106-116 cm
答案 1 :(得分:0)
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$patterns[0] = '/([0-9]+)\s([0-9]+)/';
$replacements[0] = '$1-$2';
此外,由于您正在进行单一替换,您可以这样做:
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$string = preg_replace('/([0-9]+)\s([0-9]+)/','$1-$2',$string);