The fastest and easiest way is perhaps the stripos function. It returns the position of the string inside another or false if it cannot be found:
if (false === stripos($string, '|')) { $string = null; }
Strict type comparisons require false ===
, since stripos can return zero, indicating that | located on the first char.
You can use a more sophisticated check mechanism that makes reading easier. I recommend Respect \ Validation . Usage example:
if (v::not(v::contains('|'))->validate($string)) { $string = null; }
source share