+ 1

How to calculate string which contains letters in PHP?

Hello , i will appriciet the help . Lets say i have string in PHP which is : $string = "This is 30cm of length and width of 120cm"; and i want to transforme this (keepeng the sentence same) desirible output to be "This is 11.8'' of length and width of 78.7'' i have the formula for the inches bit , i successfuly can extract just the numbers and calculate them , but thats not work for me beacuse the sentences coming from outside source (csv) file and str_replace() not helping at all :)

20th Dec 2021, 3:42 PM
Alev Mustafa
2 Answers
+ 1
$string = "This is 30cm of length and width of 120cm"; preg_replace_callback('/\d+(?:\.\d+)?/', function($m) { return round(floatval($m[0]) * 0.39370 / 0.5) * 0.5 . '"'; }, $string); I FOUND THE ANSWER
20th Dec 2021, 4:16 PM
Alev Mustafa
0
I used your formula but I got different results, So I used general way to convert cm to inch. https://code.sololearn.com/wCqw0UTOfB7l/?ref=app
20th Dec 2021, 8:22 PM
Ipang