I'm trying to write a regex that replaces everything after something that's not a space or a letter in PHP.
I currently have the following
$_product[self::NAME] = preg_replace('^[a-z .*$]+$/i', '', $_product[self::NAME]);
Milk - 50 Gallons
chocolate milk - 50 gallons
Milk 50 gallons
The It replaces the string with a blank string proves you made a typo when posting the question, and you actually have
'/^[a-z .*$]+$/i' regex.
That also means that your regex matches any string that contains one or more chars from the
[a-z .*$] set (ASCII letters, space, dot,
If you need a regex that replaces everything after something that's not a space or a letter, you need
preg_replace('/\s*[^a-z\s].*$/i', '', $_product[self::NAME]);
See the regex demo
\s*- zero or more whitespaces
[^a-z\s]- any char other than ASCII letter or whitespace
.*- any zero or more characters other than newline (add
/smodifier to also match newlines)
$- end of string.