0

I have a string that I need to explode and get the information.

Sample string:

"20' Container 1, 40' Open Container 1, 40-45' Closed Container 3"

First I am exploding the string by , and getting

"20' Container 1"
"40' Open Container 1"
"40-45' Closed Container 3"

Now I want to explode the already exploded array as well so that I get the result in below format

array[
    0 => [
        0 => "20'"
        1 => "Container"
        2 => "1"
        ]
    1 => [
        0 => "40'"
        1 => "Open Container"
        2 => "1"
        ]
    ]

The strings may vary but it is decided that the format will be same e.g. length type number

3 Answers 3

1

Loop thru exploded string with comma delimiter, then push each matches base on length type number to result array

$string = "20' Container 1, 40' Open Container 1, 40-45' Closed Container 3";

$result = [];
$pattern = '/([\d-]*\')\s(.*)\s(\d+)/';
foreach (explode(', ', $string) as $value) {
    preg_match($pattern, $value, $matches); // Match length, type, number
    $result[] = array_slice($matches, 1);   // Slice with offset 1
}

print_r($result);
Sign up to request clarification or add additional context in comments.

4 Comments

This gives me 4 indexes of array in 2nd iteration. I want 3 indexes of each array as my result set
What will be the exact pattern here in this case?
You the life saver :p
I edited the pattern to match with lengths that has range
1

You can simply explode one more time to get required array,

$str = "20' Container 1, 40' Open Container 1, 40-45' Closed Container 3";
$result = array();
$temp= explode(", ",$str);
array_walk($temp, function($item, $key) use(&$result) {
    $result[$key] = explode(" ", $item);
});
print_r($result);

Here is working demo.

1 Comment

I want 3 indexes of each array. 2nd iteration gives me 4 indexes
0

Another way to go:

$str = "20' Container 1, 40' Open Container 1, 40-45' Closed Container 3";
preg_match_all("/(\d+(?:-\d+)?')\s+([a-z\s]+)\s(\d+)(?:, )?/i", $str, $match);
for($i = 0; $i <3; $i++) {
    $final[] = array_slice(array_column($match, $i), 1);
}
print_r($final);

Output:

Array
(
    [0] => Array
        (
            [0] => 20'
            [1] => Container
            [2] => 1
        )

    [1] => Array
        (
            [0] => 40'
            [1] => Open Container
            [2] => 1
        )

    [2] => Array
        (
            [0] => 40-45'
            [1] => Closed Container
            [2] => 3
        )

)

Regex:

/               : regex delimiter
  (             : start group 1
    \d+         : 1 or more digits
    (?:-\d+)?   : non capture group, a dash followed by digits, optional
    '           : 'single quote
  )             : end group 1
  \s+           : 1 or more spaces
  (             : start group 2
    [a-z\s]+    : 1 or more alphabetic or space
  )             : end group 2
  \s+           : 1 or more spaces
  (             : start group 3
    \d+         : 1 or more digits
  )             : end group 3
  (?:, )?       : non capture group, a comma followed by space, optional
/i              : regex delimiter, case insensitive

Comments

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.