1

My string variable contains "[100][200][300][400]" data. This variable should be split it into array without brackets.

Currently I can split into $matches array with regular expression, but bracket appear in array.

I have used current expression as bellow:

preg _match_all('/\[.*?\]/', $string , $matches) 

3 Answers 3

3

why not try this:

<?php
    $str = " [100][200][300][400] ";
    $str = explode("][", trim($str, "[] "));
    print_r($str);
    exit;
Sign up to request clarification or add additional context in comments.

1 Comment

This is better solution than regex if you know that the characters you want to split on are not going to change.
0

This code maybe can help

$str = "[100][200][300][400]";
$str = explode("][",trim($str,'\[\]'));

Results in:

Array( [0] => 100 [1] => 200 [2] => 300 [3] => 400 )

Comments

0

Use brackets to choose what part of the match you want to return.

preg_match_all('/\[(.*?)\]/', $string , $matches) 

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.