regex - php preg match a string ignoring white space -


hello need change expression ignores white space

preg_match("/([a-za-z\s]{2,}\,\s)+(red|blue|green|black)$/i",$query, $matches))  $query = "honda accord, red" 

so still gets match if there white space added such as;

$query = "   honda    accord   ,    red   " 

basically need matches honda accord, red correct spacing. can see im no regexp expert:)

thanks in advance.

$query = "   honda    accord   ,    red   ";  $query = trim($query); // remove spaces @ ends // $query = preg_replace('/\s+/', ' ', $query); // make sure there aren't multiple spaces // $query = preg_replace('/\s?,\s?/', ', ', $query); // enforce 'word, word' format // preg_match("/([a-za-z\s]{2,}\,\s)+(red|blue|green|black)$/i",$query, $matches); 

this formats string have standard form, if want matches sufficient add \s* needed.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -