Take a look at this answer.
To quote:
re_valid = r"""
# Validate a CSV string having single, double or un-quoted values.
^ # Anchor to start of string.
\s* # Allow whitespace before value.
(?: # Group for value alternatives.
'[^'\\]*(?:\\[\S\s][^'\\]*)*' # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*" # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)* # or Non-comma, non-quote stuff.
) # End group of value alternatives.
\s* # Allow whitespace after value.
(?: # Zero or more additional values
, # Values separated by a comma.
\s* # Allow whitespace before value.
(?: # Group for value alternatives.
'[^'\\]*(?:\\[\S\s][^'\\]*)*' # Either Single quoted string,
| "[^"\\]*(?:\\[\S\s][^"\\]*)*" # or Double quoted string,
| [^,'"\s\\]*(?:\s+[^,'"\s\\]+)* # or Non-comma, non-quote stuff.
) # End group of value alternatives.
\s* # Allow whitespace after value.
)* # Zero or more additional values
$ # Anchor to end of string.
"""
Or the usable form (since JS can't handle multi-line regex strings):
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
It can be called using RegEx.test()
if (!re_valid.test(text)) return null;
The first match looks for valid single-quoted strings. The second match looks for valid double-quoted strings, the third looks for unquoted strings.
If you remove the single-quote matches it is an almost 100% implementation of a working IETF RFC 4810 spec CSV validator.
Note: It might be 100% but I can't remember whether it can handle newline chars in values (I think the [\S\s] is a javascript-specific hack to check for newline chars).
Note: This is a JavaScript-only implementation, there are no guarantees that the RegEx source string will work in PHP.
If you're planning on doing anything non-trivial with CSV data, I suggest you adopt an existing library. It gets pretty ugly if you're looking for a RFC-compliant implementation.