<?php
$re = "/(\\w+)(?=\\s+\\d)/m"; 
$str = "width 40cm height 70cm\nwidth: 40cm height 70cm"; 
 
$result = preg_replace($re, "$1:", $str);
print_r($result);