DrTrans Posted December 15, 2013 Share Posted December 15, 2013 $string = "<214.27080, 204.10100, 2500.51300>"; need it parsed into 214/204/2500 Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/ 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 Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462350 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. Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462357 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); Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462361 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, "<"); Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462362 Share on other sites More sharing options...
Psycho Posted December 15, 2013 Share Posted December 15, 2013 On 12/15/2013 at 6:59 AM, dungpt29 said: 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. Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462367 Share on other sites More sharing options...
dungpt29 Posted December 15, 2013 Share Posted December 15, 2013 Quote 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, Quote $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? Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462371 Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 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 Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462372 Share on other sites More sharing options...
Ch0cu3r Posted December 15, 2013 Share Posted December 15, 2013 On 12/15/2013 at 4:13 PM, Ch0cu3r said: 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 Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462374 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); Link to comment https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.