PHP字符串比较,删除空格

I am looping through two datasets and the string comparisons are working except for one row:

ALTER TABLE `tablename` CHANGE `field`  enum('P','W','M', 'G') 

ALTER TABLE `tablename` CHANGE `field`    enum('P','W','M','G')

I believe it is because of the space in between 'M', 'G' in the first string. I want to remove this but still keep the spaces in the first part of the string.

You can compress all sequences of multiple spaces to a single space:

if (preg_replace('/\s{2,}/', ' ', $string1) == preg_replace('/\s{2,}/', ' ', $string2))

However, this won't help if the two strings are:

ALTER TABLE `tablename` CHANGE `field`  enum('P','W','M', 'G') 
ALTER TABLE `tablename` CHANGE `field`  enum('P', 'W', 'M', 'G') 

These are equivalent in SQL because the space after comma is optional. If you want to be able to handle this, you'll need to write a more elaborate parser that understands SQL syntax.