skip to Main Content

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);

2

Answers


  1. If I understand correctly, you want to match two values from the string "gets(183,);inc();" for="text">Sweet" using regular expressions. Here’s an example regex that should work:

    gets((d+),);inc();.*for="([^"]+)"
    

    This regex has two capture groups:

    1. (d+) captures one or more digits inside the gets() function.
    2. "([^"]+)" captures one or more characters inside the for attribute, excluding the double quotes.

    Here’s an example PHP code to use this regex and extract the values:

    $string = 'gets(183,);inc(); for="text">Sweet';
    $pattern = '/gets((d+),);inc();.*for="([^"]+)"/';
    if (preg_match($pattern, $string, $matches)) {
        $number = $matches[1]; // Captured value inside gets() function
        $text = $matches[2]; // Captured value inside the for attribute
        echo "Number: $numbern";
        echo "Text: $textn";
    } else {
        echo "No match found.n";
    }
    

    This code will output:

    Number: 183
    Text: text
    
    Login or Signup to reply.
  2. 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
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search