DrTrans Posted January 14, 2014 Share Posted January 14, 2014 Looking on how to format this: $string = "(215.354100, 218.613800, 2501.263000)" into (215,218,2501) Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 14, 2014 Share Posted January 14, 2014 (edited) Trim, explode and floor $string = '(215.354100, 218.613800, 2501.263000)'; // trim the () from the start/end of string and explode on comma and space $data = explode(', ', trim($string, '()')); // apply floor to round number down to nearest whole integer (removes the decimal) $data = array_map('floor', $data); // create the new string $new_string = sprintf('(%s)', implode(', ', $data)); echo "$string<br />$new_string"; Or a simple preg_replace with regex $new_string = preg_replace('~(\.\d+)~', '', $string); Edited January 14, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
DrTrans Posted January 14, 2014 Author Share Posted January 14, 2014 Thank You so much. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 14, 2014 Share Posted January 14, 2014 (edited) floor() easier, just another way <?php $my_string = "(215.354100, 218.613800, 2501.263000)"; $my_array = array( "215.354100", "218.613800", "2501.263000" ); function format_numbers($input = '') { if ($input == '') { return $input; } if (!is_array($input)) { $input = explode(",", $input); } $first_numbers = array(); foreach ($input as $numbers) { $numbers = trim($numbers); $numbers = str_replace(array( "(", ")" ), '', $numbers); $end_numbers = end(explode(".", $numbers)); $first_numbers[] = str_replace(".$end_numbers", '', $numbers); } $formatted = "(" . implode($first_numbers, ",") . ")"; return $formatted; } echo format_numbers($my_string) . "<br />"; echo format_numbers($my_array) . "<br />"; echo format_numbers() . "<br />"; ?> Edited January 14, 2014 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.