1

I have a string where there are arrays separated by {|} like this :

$string = "Hi, {Mr.|Mrs.} {Alvin|Dale}! Good Morning!";

One of the many things I've tried :

$string = "Hi, {Mr.|Mrs.} {Alvin|Dale}! Good Morning!";
preg_match_all("/[()=]|\\{[^\}]+\\}|[+-]|[^=]+$/", $string, $matches);

Expected result:

$result = array ( 
0 => array ( 0 => 'Hi, '), 
1 => array ( 0 => 'Mr.', 1 => 'Mrs.'),
2 => array ( 0 => ' '), 
3 => array ( 0 => 'Alvin', 1 => 'Dale'), 
4 => array ( 0 => '! Good Morning!')
);

Actual result:

$result = array ( 
0 => 'Hi, {Mr.|Mrs.} {Alvin|Dale}! Good Morning!'
);
2
  • What's your use case here? Are you trying to do translation or is this a part of some custom template? Commented Aug 7, 2021 at 15:15
  • i want to show all possible of $string like this question link Commented Aug 8, 2021 at 2:49

3 Answers 3

1

You could split the string on matching from an opening curly till a closing curly brace, and keep the values you split on using that pattern in a capture group for the inner part of the curly braces

Then you can use array_map and split on |.

For example

$pattern = "/{([^{}]+)}/";
$string = "Hi, {Mr.|Mrs.} {Alvin|Dale}! Good Morning!";
$result = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

$result = array_map(function($x) {
    return explode('|', $x);
}, $result);

Output

array (
  0 => 
  array (
    0 => 'Hi, ',
  ),
  1 => 
  array (
    0 => 'Mr.',
    1 => 'Mrs.',
  ),
  2 => 
  array (
    0 => ' ',
  ),
  3 => 
  array (
    0 => 'Alvin',
    1 => 'Dale',
  ),
  4 => 
  array (
    0 => '! Good Morning!',
  ),
)

See a PHP demo

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

Comments

1

PHP >= 7.4:

$string = "Hi, {Mr.|Mrs.} {Alvin|Dale}! Good Morning!";

preg_match_all('~{[^}]*}|[^{]+~', $string, $matches);

$result = array_map(
    fn($m) => $m[0] === '{' ? explode('|', trim($m, '{}')) : $m,
    $matches[0]
);

Comments

0

Could you explain how $string gets generated as your array is incorrect

For example those {} gets generated via json: EG:

$string = array(
    0 => 'Hi, ',
    1 => array(
        0 => 'Mr.',
        1 => 'Mrs.'
    )
);

$jsonOut = json_encode($string); // encodes array to json format

print_r('Json String: ' . $jsonOut);
print "\r\n";
print_r('Back To array format:');
print "\r\n";
print_r(json_decode($jsonOut, true)); // json_decode(#jsonString, true) brings back the original php array

In the example above, you will get the following outputs (all tested)

Json String: ["Hi, ",["Mr.","Mrs."]]
Back To array format:
Array
(
    [0] => Hi, 
    [1] => Array
        (
            [0] => Mr.
            [1] => Mrs.
        )

)

I have used the data you provided to give you a little bit of an understanding (with example), I cannot do the entire script for you though.

PHP Arrays PHP Json

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.