DrTrans Posted December 15, 2013 Share Posted December 15, 2013 $string = "<214.27080, 204.10100, 2500.51300>"; need it parsed into 214/204/2500 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted December 15, 2013 Share Posted December 15, 2013 You could use explode(): http://www.php.net/explode Quote Link to comment Share on other sites More sharing options...
DrTrans Posted December 15, 2013 Author Share Posted December 15, 2013 I can't seem to figure it out with explode, because of the multi character, and whole number. please assist further if you could. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 15, 2013 Share Posted December 15, 2013 $string = "<214.27080, 204.10100, 2500.51300>"; //Convert string into array - splitting on the commas $parts = explode(',', $string); //Iterate over each value in the array foreach($parts as &$part) { //Trim spaces and "<>" from each value and return the rounded down value $part = floor(trim($part, " <>")); } //Convert the parts of the array to a string using '/' as delimiter $newstring = implode('/', $parts); Quote Link to comment Share on other sites More sharing options...
dungpt29 Posted December 15, 2013 Share Posted December 15, 2013 Another solution to solve your problem could be as follows: $a1 = array(); $a2 = array(); $a3 = array(); // $txtInput contains your input data that is <214.27080, 204.10100, 2500.51300> $a1 = explode(",", $txtInput); $a1_length = count($a1); for ($i=0; $i < $a1_length; $i++) { $a2 = explode(".", trim($a1[$i])); array_push($a3, $a2[0]); array_pop($a2); array_pop($a2); } $str = implode("/",$a3); $str = trim($str, "<"); Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 15, 2013 Share Posted December 15, 2013 Another solution to solve your problem could be as follows: $a1 = array(); $a2 = array(); $a3 = array(); // $txtInput contains your input data that is <214.27080, 204.10100, 2500.51300> $a1 = explode(",", $txtInput); $a1_length = count($a1); for ($i=0; $i < $a1_length; $i++) { $a2 = explode(".", trim($a1[$i])); array_push($a3, $a2[0]); array_pop($a2); array_pop($a2); } $str = implode("/",$a3); $str = trim($str, "<"); Except for the fact that it leaves the '<' at the beginning. Not to mention there is a lot of wasted code. There is no reason to do array_pop() to remove the other values from the array since $a2 would be redefined on the next iteration of the loop. Quote Link to comment Share on other sites More sharing options...
dungpt29 Posted December 15, 2013 Share Posted December 15, 2013 (edited) Except for the fact that it leaves the '<' at the beginning. Not to mention there is a lot of wasted code. There is no reason to do array_pop() to remove the other values from the array since $a2 would be redefined on the next iteration of the loop. Thanks for your remarks. I am striving to write the neater code in the future. And I have a question for your solution. Assuming that the input string contains the negative numbers. For example: $string = "<214.27080, -204.88100, 2500.51300>"; Based on your code, $part = floor(trim($part, " <>")); I guess the output string will be: $newstring = "214/-205/2500"; Based on OP's requirement, the output string must be: $newstring = "214/-204/2500"; What do you think of this case, Psycho? Edited December 15, 2013 by dungpt29 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 (edited) Then you need to use ceil for negative numbers $part = trim($part, " <>"); $part = ($part > 0) ? floor($part) : ceil($part); floor rounds numbers down. -205 is lower than -204.1 Edited December 15, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 floor rounds numbers down. -205 is lower than -204.1I said that wrong. -204.1 is lower than -204. So the nearest whole number is -205 Quote Link to comment Share on other sites More sharing options...
ignace Posted December 15, 2013 Share Posted December 15, 2013 Simply use intval $string = "<214.27080, 204.10100, 2500.51300>"; //Convert string into array - splitting on the commas $parts = explode(',', $string); //Iterate over each value in the array foreach($parts as &$part) { //Trim spaces and "<>" from each value and return the rounded down value $part = intval(trim($part, " <>")); } //Convert the parts of the array to a string using '/' as delimiter $newstring = implode('/', $parts); 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.