php - regex to format operators in a expression -


i need operators (arithmetic, comparison, logical...) in string...

((value+10)*10)>=300   ||   (array[key]-20==0&&"foo==bar"!=="") 

...and add single whitespace before , after them (if doesn't exists)...

((value + 10) * 10) >= 300 || (array[key] - 20 == 0 && "foo==bar" !== "") 

...ignoring between quotes or single quotes.

aditional rules:

  • add space before pre-in/decrement operators
  • add space after post-in/decrement operators

sounds easy can't how using regex in php. if can help!

i'm trying $str = preg_replace('(?<=[\w\]\)\s\"\'])\!=(?=[\w\]\)\s\"\'])', ' != ', $str); every operator, don't think it's idea.

this pattern works pcre engines, believe php is

"[^"\r\n]*"(*skip)(*f)|\s*([\-\/+*=|<>!&]+)\s* 

demo


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -