$array = split(/\D/,$var);

In perl this code will split a variable on each non-numeric character in the variable.

$var = "12 13,15n20";
$array = split(/\D/,$var);

$array will look like this:
(12,13,15,20)

How can this be done in php?

Thanks,
Raoul

    the explode() function allows you to split a string into an array by a specified character or string.
    For example...
    $data = "I went to the park";

    could be put through the explode() function to split the string by a " ".

    $split = explode(" ", $data);

    the $split array would look like this..
    [0] => I
    [1] => went
    [2] => to
    [3] => the
    [4] => park

    is that what you were trying to do?
    skripty

      Thanks,

      yes, but I want to explode on any character that is non-numeric (0-9).

      How to do that?
      raoul

        What you would have to do is replace any non-numeric character with a pipe charachter ("|"). Then explode that. The code to do this is below.

        <?
        $string = "32h14g15y34j98l78";
        $length = strlen($string);
        for($i=1;$i<=$length;$i++){
        $temp = substr($string, $i, 1);
        if (is_nan($temp)){
        $string = str_replace($temp, "|", $string);
        }
        }
        $array = explode("|", $string);
        $arrayCount = count($array);
        for($i=0;$i<$arrayCount;$i++){
        echo $array[$i];
        echo"<br>";
        }
        ?>

        This code is not tested but it is a solution to the problem

        Hope this helps
        skripty

          thanks,

          I am afraid I can't use the is_nan function as it is supported from version 4.2. My server is running 4.1... something

          But i will work around this somehow.

          Thanks a lot for the help!

            I think this is what you are looking for:

            $var = "12 13,15n20";
            $array = preg_split("~\D~",$var);

            And there you go 🙂

            -Josh B

              Applause for JoshB.
              That is what i was looking for but i couldnt find it in the manual if my life depended on it. Thanks

                Write a Reply...