2

For example:

$string = "AND (pr.StatusCode <> 'U') AND ((pr.pkBrand = 9) OR (pr.pkBrand = 70)) AND ((pr.pkCategory = 55) OR (pr.pkCategory = 56)) AND (pr.StatusCode <> 'D')";

I would need to pull the values associate with pkBrand i.e. 9 & 70 to push into an array for use elsewhere and the values of pkCategory i.e. 55 & 56 into a separate array again for use elsewhere.

How can I do this please.

2 Answers 2

3

Maybe you can do something like this:

$string = "AND (pr.StatusCode <> 'U') AND ((pr.pkBrand = 9) OR (pr.pkBrand = 70)) AND ((pr.pkCategory = 55) OR (pr.pkCategory = 56)) AND (pr.StatusCode <> 'D')";
preg_match_all("/pr.pkBrand = (\d+)|pr.pkCategory = (\d+)/", $string, $matches, PREG_SET_ORDER);

The $matches array will look like this:

array(4) { [0] => array(2) { [0] => string(14) "pr.pkBrand = 9" [1] => string(1) "9" } [1] => array(2) { [0] => string(15) "pr.pkBrand = 70" [1] => string(2) "70" } [2] => array(3) { [0] => string(18) "pr.pkCategory = 55" [1] => string(0) "" [2] => string(2) "55" } [3] => array(3) { [0] => string(18) "pr.pkCategory = 56" [1] => string(0) "" [2] => string(2) "56" } }

Then you can extract the values you want from this.

Sign up to request clarification or add additional context in comments.

Comments

1

this is much less concise but it aims to work regardless of the field name:

<?php
$string = "AND (pr.StatusCode <> 'U') AND ((pr.pkBrand = 9) OR (pr.pkBrand = 70)) AND ((pr.pkCategory = 55) OR (pr.pkCategory = 56)) AND (pr.StatusCode <> 'D')";
$newString=trim(str_replace(')','',str_replace('(','',str_replace('OR','',str_replace('AND','',$string)))));
$tab=explode('pr.',$newString);
foreach($tab as $value) {
 $value=trim(str_replace(' ','',str_replace('\'','',$value)));
 $tab2[]=$value;
}
/*echo '<pre>';
var_dump($tab2);
echo '</pre>';*/
$operators=['!=','<>','<','>','='];
foreach ($tab2 as $index=>$value) {
 foreach ($operators as $operator) {
    $found=0;
    $explode=explode($operator,$value);
     if (count($explode)==1) {}//not the good one
     else {
      $found=1;
      break;
     }
 }
 if ($found==1) $newTab[$explode[0]][$index]=$explode[1];
}


echo '<pre>';
var_dump($newTab);
echo '</pre>';

?>

Will give you :

array(3) {
  ["StatusCode"]=>
  array(2) {
    [1]=>
    string(1) "U"
    [6]=>
    string(1) "D"
  }
  ["pkBrand"]=>
  array(2) {
    [2]=>
    string(1) "9"
    [3]=>
    string(2) "70"
  }
  ["pkCategory"]=>
  array(2) {
    [4]=>
    string(2) "55"
    [5]=>
    string(2) "56"
  }
}

5 Comments

your code gives me an error: Parse error: syntax error, unexpected '[' in ... for this line $operators = ['!=','<>','<','>','='];
no error for me. Can you give me the complete error message ?
Parse error: syntax error, unexpected '[' in /Users/***/Sites/***/_plugins/site-pages/public/add-a-model-pages/manage-models.php on line 547 line 547 is $operators=['!=','<>','<','>','='];
ok you certainly use php <5.4. Replace $operators=['!=','<>','<','>','=']; with $operators = array('!=','<>','<','>','=');
You are correct the server runs 5.3.26, your revised code runs a treat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.