Hello I need to change this expression so it ignores white space
preg_match("/([a-zA-Z\s]{2,}\,\s)+(RED|BLUE|GREEN|BLACK)$/i",$query, $matches))
$query = "Honda Accord, RED"
so it sti开发者_如何学运维ll gets the match even if there is white space added such as;
$query = " Honda Accord , RED "
Basically i need the matches back as Honda Accord, RED with correct spacing. As you can see im no regexp expert:)
Thanks in advance.
$query = " Honda Accord , RED ";
$query = trim($query); // remove spaces at the ends //
$query = preg_replace('/\s+/', ' ', $query); // make sure there aren't multiple spaces //
$query = preg_replace('/\s?,\s?/', ', ', $query); // enforce the 'word, word' format //
preg_match("/([a-zA-Z\s]{2,}\,\s)+(RED|BLUE|GREEN|BLACK)$/i",$query, $matches);
This formats your string so that it will have a standard form, but if you just want to get the matches it is sufficient to add \s*
where needed.
You need whitespace before and after color and also at the beginning:
^\s*([a-zA-Z\s]{2,},\s)+\s*(RED|BLUE|GREEN|BLACK)\s*$
Rubular link
精彩评论