2

I have a for loop designed to extract all the numbers from varying strings I supply to it. I am saving the extracted numbers in an array. My code is as follows:

$length = strlen($RouteString); 
$data = [];
$index = 0;     
for($i = 0; $i < $length; $i++)
{           
    $j = 1;
    $count = 0;
    while(is_numeric(substr($RouteString,$i,$j)) == true)
    {
        $data[$index] = substr($RouteString,$i,$j);
        $j = $j+1;          
    }

    if(is_numeric(substr($RouteString,$i,1)) == true)
    {
        $index = $index + 1;
    }
}

$Routestring is set as: "B12-1234-U102-D4-11-19-E" and should give the results of $data=[12,1234,102,4,11,19] but instead it gives $data=[12,2,1234,234,34,4,102,02,2,4,11,1,19,9].

I have tried fixing the problem by adjusting the $index but it doesn't work. I can't figure out how to fix this.

Any advice would be greatly appreciated

3
  • Would it work if you just replaced all non numbers with a single pipe, then exploded by the pipes? I think that might be a better, quicker approach to the issue, but obviously does not answer your question as it currently is. Commented Oct 24, 2019 at 21:19
  • @zbee I tried your method but it creates empty array entries, That's the same reason I used the $index in my code instead of $i. Commented Oct 24, 2019 at 21:39
  • See abraCadaver's answer for what I had meant. It returns exactly what you said you were aiming for, but in a much more concise manner. Commented Oct 24, 2019 at 22:06

3 Answers 3

2

There are many ways to do this easier, here is one:

preg_match_all('/\d+/', $string, $matches);

Match on 1 or more digits \d+. Your array will be in $matches[0].

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much
0

This is how I would do it:

$str = 'B12-1234-U102-D4-11-19-E';
$data = array_map('intval', array_filter(preg_split("/\D+/", $str)));
  • preg_split will return an array with only numbers.

  • array_filter will remove all the nulls from the result.

  • array_map will transform all the results to integers.

And if you have a problem with the keys you can use array_values to reset them.

Sandbox

Comments

0

If there's no other variables involved, this should work:

UPDATED

$str = "B12-1234-U102-D4-11-19-E";
$result = preg_split("/\D/", $str, -1, PREG_SPLIT_NO_EMPTY);

4 Comments

Please explain your usage of preg_split().
I tried this method that you suggested but it creates empty array entries, That's the same reason I used the $index in my code instead of $i.
There is a PREG_SPLIT_NO_EMPTY flag but preg_split is probably not the best tool for this job.
@Stephan can you show your code that is producing empty array entries? Both this code and that in the other answer provide the result set you said you were trying to attain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.