Jump to content

Parsing this string


DrTrans

Recommended Posts

 

$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

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

 

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

 

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?

Link to comment
https://forums.phpfreaks.com/topic/284769-parsing-this-string/#findComment-1462371
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.