Line training in php

I have a line like this,

$inp1 = "3 doses at 0[0,0], 1-2 and 6 Month[6,1] [3,2])";

      

in this internally, getting the values ​​of the square bracket. How can I take these values ​​in square brackets? any function returns a string like this

[0,0] [6,1] [3,2]

Thanks in advance for your help.

+2


a source to share


2 answers


preg_match_all('/\[\d+,\d+\]/', $inp1, $matches);
$result = implode(' ', $matches[0]);

      



+5


a source


you can try with preg_replace function using regex:

$s = "3 doses at 0[0,0], 1-2 and 6 Month[6,1] [3,2])";
$s = preg_replace("/[^\[]*(\[[^\]]*\])[^\[]*/","$1",$s);
echo $s;

      



Outputs

[0,0][6,1][3,2]

      

+1


a source







All Articles