Edit: Not looking for anyone to write code for me, just looking for advice on which way is best to achieve what I’m trying to do.
I can think of several ways of doing this, but I was just wondering if there’s more elegant ways.
For example, I want to know if “abc” is in “cat,dog,abc,xyz”
These are how I’ve done it before…
in_array($key, explode(',', $value));
(strpos($value, ','.$key) !=== FALSE) || (strpos($value, $key.',') !=== FALSE);
preg_match('/(\,'.$key.'|'.$key.'\,)/', $value) !== 0 // Untested, but.. just for the general idea.
Any words of wisdom for better ways, Stack Overflow?
The first method is probably the most reliable/accurate, and the easiest to read/understand. However, you should use str_getcsv() instead of
explode(), because commas could be inside the comma-delimited data. Example where it breaks down:The other two methods are error-prone and have edge-cases which cause false-positives. They’re very unreliable.