Search code examples
phpregexregex-group

Regex match two value from string php


Hi i am trying to match two value by regex two conditions, But not able to do.

string is

MorText "gets(183,);inc();" for="text">Sweet" Mo

output trying is array

[
  183,
  "Sweet"
]

php regex code is

 preg_match_all('/gets\((.*?)\,|\>(.*?)\"/', $string, $matches);

Solution

  • To achieve your wanted output you can use:

    /gets\((\d+),.*?>(.*?)\"/
    

    PHP-Example:

    $string = 'MorText "gets(183,);inc();" for="text">Sweet" Mo';
    
    preg_match_all('/gets\((\d+),.*?>(.*?)\"/', $string, $matches);
        
    print_r(array_merge($matches[1],$matches[2]));
    

    Outputs:

    Array
    (
        [0] => 183
        [1] => Sweet
    )