I have a string val of H: 35" V: 24 1/2"
I can extract the 35
and 24
perform a metric conversion calculation then,
(and this is where i'm stuck),
remake the string to look like H: 35" (89 cm) V: 24 1/2" (62 cm)
?
I found regex that could pull the int by
$str = 'H: 35" V: 24 1/2"';
preg_match_all('!\d+!', $str, $matches);
print_r($matches, true);
//Array ( [0] => 35 [1] => 24 [2] => 1 [3] => 2 ) )
But after this, how can I remake the original string and insert the array values after each of the pieces?
FINAL
thank you @AbraCadaver for the answer, however I found the fraction 24 1/2
was not getting calculated so here was my final
$str = preg_replace_callback('!(\d+( \d+/\d+)?")!',
function($matches) {
if(isset($matches[2])){
$fraction = $matches[2];
if( strpos( $fraction, '/' ) !== false ) {
$ints = explode('/', $fraction);
$fraction = $ints[0] / $ints[1];
}
}
$new = isset($matches[2]) ? $matches[1] + $fraction : $matches[1];
$new *= 2.54;
return $matches[1] . " (" . round($new) . " cm)" . "<br>";
}
, $str);