Reputation: 424
A piece of code would explain my problem
$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);
I need to put a - between numbers, like:
$string = '53-69 cm; 988-2460 g; wing 106-116 cm';
How can I put the - on the replacement?
Thanks
Upvotes: 1
Views: 80
Reputation: 197682
Hope this gives you some help:
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$pattern = '/(\d+)\s(\d+)/';
$replacement = '$1-$2';
echo preg_replace($pattern, $replacement, $string);
Output:
53-69 cm; 988-2460 g; wing 106-116 cm
Upvotes: 4
Reputation: 454970
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$patterns[0] = '/([0-9]+)\s([0-9]+)/';
$replacements[0] = '$1-$2';
Also since you are doing single replacement you can just do:
$string = '53 69 cm; 988 2460 g; wing 106 116 cm';
$string = preg_replace('/([0-9]+)\s([0-9]+)/','$1-$2',$string);
Upvotes: 0