Home  >  Q&A  >  body text

Find multiple occurrences of a pattern, using regular expressions

I got this string:

if(Condition A==Value A-AND-Condition B==Value B-OR-Condition C==Value C)

I want an array containing:

array(3) {
  [0]=>
  string(...) "conditionA==valueA"
  [1]=>
  string(...) "conditionB==valueB"
  [2]=>
  string(...) "conditionC==valueC"
}

I'm currently using this pattern:

preg_match("/^if\((. )-(. )\)/U", $current_step_data_exploded[0], $if_statements);

But it doesn't satisfy the third condition correctly. Can anyone help me?

P粉186017651P粉186017651208 days ago3578

reply all(1)I'll reply

  • P粉139351297

    P粉1393512972024-02-27 21:14:22

    $string = "if(conditionA==valueA-AND-conditionB==valueB-OR-conditionC==valueC)";
    $match = preg_match('/^if\((.+)\)$/', $string, $if);
    if ($match) {
      $conditions = preg_split('/\-(AND|OR)\-/', $if[1]);
      print_r($conditions);
    } else {
      echo "no matches.";
    }

    reply
    0
  • Cancelreply