Home > OS >  Split string into two
Split string into two

Time:08-15

So I have a php page which GETs a form with a string looking like this:

FE:99792913V00.00.01.00038;SW:99792937V00.00.01.00022 I should like to make two string instead of one, looking like this:

    $string1 = "'FE', 'SW'";
    $string2 = "'99792913V00.00.01.00038', '99792937V00.00.01.00022'";

The only way I can think of doing this is by splitting with explode() and then implode it back to a string, but is this the best way?

$VersionRVTM = $_GET['VersionRVTM'];
$string1 = explode(";", $VersionRVTM);echo"<br><br>";

foreach ($string1 as $key ) {
  $array = explode(":", $key);
  $array_1[] = $array[0];
  $array_2[] = $array[1];
}
echo "'".implode("', '", $array_1)."'";echo"<br>";
echo "'".implode("', '", $array_2)."'";

//output: 'FE', 'SW'
//output: '99792913V00.00.01.00038', '99792937V00.00.01.00022'

CodePudding user response:

Yes, explode() and implode() are fast, reliable, and made for this sort of requirement.

  •  Tags:  
  • php
  • Related