regex - How to do a exact word replacment in a string using PHP -


i using numbers variable in string construct logic.

here example of string have

(1 , 2) 2  ,   1 (1 , 2) or  (3 , 4) 1 , (2  or  3) 

then have array looks this

 $this->clause = array(           '1' => (object) array('fieldid' => 'id',                                 'value' => 1),            '2' => (object) array('fieldid' => 'cost',                                 'value' => 132),            '3' => (object) array('fieldid' => 'name',                                 'value' => "jay's llc"),            '4' => (object) array('fieldid' => 'first',                                 'value' => "mike"),         ); 

i trying replace values in string (1, 2, 3, , 4) corresponding values in array.

here how strings need once done

first example "since logic string not contain clause 3 , 4 both added using 'and' logic @ end of string"

(1 , 2) becomes ( id = '1' , cost = '132' ) , name = 'jay\'s llc' , first = 'mike' 

second example "since logic string not contain clause 3 , 4 both added using 'and' logic @ end of string"

    2  ,   1 becomes     cost = '132' , id = '1' , name = 'jay\'s llc' , first = 'mike' 

third example "since logic string not contain clause 4 added using 'and' logic @ end of string"

(1 , 2) or  (3 , 4) becomes ( id = '1' , cost = '132' ) or (name = 'jay\'s llc') 

last example

1 , (2  or  3) , 4 becomes id = '1' (cost = '132' or name = 'jay\'s llc') , first = 'mike' 

here have tried

private function getallwhereclausestatements() {     $query = '';     $totalclause = count($this->clause);      if( $totalclause > 0 ){          $query .= 'where ';          if( !empty($this->clauselogic) ){             $logic = '';             $tmplogic = $this->clauselogic;             foreach($this->clause $key => $cl){                 $tmplogic .= preg_replace('/\b'.$key.'\b/u', $this->getwhereclause($cl), $tmplogic);                 $this->clause->isused = 1;             }              if(! empty($logic) ){                 $query .= ' ( ' . $logic . ' ) ';             }              foreach($this->clause $key => $cl){                 if( !isset($this->clause->isused) || !$this->clause->isused){                     $query .= ' , ' . $this->getwhereclause($cl);                     $this->clause->isused = 1;                 }             }         }     }      return $query; }    private function getwhereclause($cl) {     return $cl->fieldid . ' = ' . $cl->value; } 

i have 2 problems need with

  1. i "warning: attempt assign property of non-object" on line $this->clause->isused = 1;
  2. i trying handle last example "i.e. 1 , (2 or 3)" following

    1 , (2 or 3) 1 , (2 or name = 'jay\'s llc' ) 1 , (2 or 3) 1 , (2 or name = 'jay\'s llc' ) 1 , (cost <> '132' or 3) 1 , (cost <> '132' or name = 'jay\'s llc' ) 1 , (cost <> '132' or 3) 1 , (cost <> '132' or name = 'jay\'s llc' ) id = '1' , (2 or 3) id = '1' , (2 or name = 'jay\'s llc' ) id = '1' , (2 or 3) id = '1' , (2 or name = 'jay\'s llc' ) id = '1' , (cost <> '132' or 3) id = '1' , (cost <> '132' or name = 'jay\'s llc' ) id = '1' , (cost <> '132' or 3) id = '1' , (cost <> '132' or name = 'jay\'s llc' )

the issue seems how trying work replacement

how correct 2 issues having?

edited

i fixed first issue line changing

$this->clause->isused = 1;

to

$this->clause[$key]->isused = 1;

i found , fixed problem

private function getallwhereclausestatements() {     $query = '';     $totalclause = count($this->clause);      if( $totalclause > 0 || ! empty($this->baseclientid) ){          $query .= 'where ';          if( !empty($this->clauselogic) ){             $logic = $this->clauselogic;             $chararray = array();             foreach($this->clause $key => $cl){                 $chararray[] = $key;             }               $charlist = implode(',', $chararray);              if( !empty($charlist) ){                 $wordscount = str_word_count($logic, 1, $charlist);             } else {                 $wordscount = str_word_count($logic, 1);             }              $words = array_count_values($wordscount);              foreach($this->clause $key => $cl){                  if(isset($words[$key]) && $words[$key] > 0){                     $logic = preg_replace('/\b'.$key.'\b/u', $this->getwhereclause($cl) , $logic);                     $this->clause[$key]->isused = 1;                                     }              }              if(! empty($logic) ){                 $query .= ' ( ' . $logic . ' ) ';             }              foreach($this->clause $key => $cl){                 if( !isset($cl->isused) ){                     $query .= ' , ' . $this->getwhereclause($cl);                     $this->clause[$key]->isused = 1;                 }             }         }      }      return $query; } 

Comments