Search code examples
phpregexpreg-matchpreg-match-all

How to extarct the string words and special characters between curly brackets in php preg_match


I have String like this

$string ="this is test {username} ,{password@123} and Other {asdfg@#$}"

I want this string as array format as follows

[1] => Array
        (
            [0] => username
            [1] => password@123
            [2] => asdfg@#$
        )

Solution

  • $re = "/\{(.*?)\}/";
    $str = "this is test {username} ,{password@123} and Other {asdfg@#\$}";
    
    preg_match_all($re, $str, $matches);
    
    print_r($matches);
    

    this gives what you want