hey there, i have the following string:

$str = "HEMSKY, ALES +1 15:33 18 :51 6:31 0:05 8:57 1 1"

and i want to use preg_split to break it into separate array elements.... i think the following code should work, but it doesnt

$str = "HEMSKY, ALES +1 15:33 18 :51 6:31 0:05 8:57 1 1"
$array = preg_split("/[,\s]/", $str, 8)

nothing gets returned.... what am i doing wrong?

    It worked fine for me after I put semi-colons at the end of each line. However, to avoid an empty array element, I'd suggest the following modification of the regex:

    <?php
    $str = "HEMSKY, ALES +1 15:33 18 :51 6:31 0:05 8:57 1 1";
    $array = preg_split("/[,\s]+/", $str, 8);
    // show result:
    echo "<pre>".print_r($array, TRUE)."<pre>\n";
    ?>
    

      Works for me

      $str = "HEMSKY, ALES +1 15:33 18 :51 6:31 0:05 8:57 1 1" ;
      echo "<br><br><pre>", $str, "<br><br>";
      $array = preg_split("/[,\s]/", $str, 8) ;
      print_r($array);
      echo "</pre>";
      

      Yields

      HEMSKY, ALES +1 15:33 18 :51 6:31 0:05 8:57 1 1

      Array
      (
      [0] => HEMSKY
      [1] =>
      [2] => ALES
      [3] => +1
      [4] => 15:33
      [5] => 18
      [6] => :51
      [7] => 6:31 0:05 8:57 1 1
      )

      Are you sure you're not getting parse errors because you forgot the ending semi colons?

      Don

        yeah i was just being silly.... it wasnt the missing semicolons though... i had renamed the $str to something else after i originally initialized it, so it was reading a different string

        thx for the help though guys

          Write a Reply...