sql - PHP regexp - remove unused constrain from query string -


background

i working on small piece of code iterate through fields of dynamically created form , create database select them.

select some_col some_tab col1=%fieldname1 , col2=%fieldname2 , col3=%fieldname3 

i iterate through fields of form , replacing "%fieldname?" value stored in particular field. problem not fields mentioned in query string listed in form. after replacing field names values, want remove rest of them query string.

question

i want remove string starts "and ", ends " " , contains "=%". can me, please?

use this:

$replaced = preg_replace('~and.*?=%\s*[ ]~', '', $yourstring); 

explanation

  • and matches literal and
  • .*? lazily matches to...
  • the literal =%
  • \s* matches chars not spaces
  • [ ] matches 1 space (the brackets not needed make easier spot)
  • we replace empty string

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -