I have a snippet of code that detects whether a string contains a semicolon a or not:
if (ereg(";",$str)) { }
How can I also expand on that and see if either a semicolon or a comma present?.. After looking around I suppose I need to use regular expressions here. Or am I getting it all wrong?
😕
you do not need regex for this
$string = 'this string has a semicolon; and a comma,'; if (strpos($string, ';') !== false || strpos($string, ',') !== false) { echo 'semicolon or comma was found'; }
Perfect.
I was actually thinking of something like: [, ;] but i'm not sure about that sort of things...
One more question. Can I add an exeption or two?
Let's say I want to exclude the following:
, MD
or
, M.D.
How would i integrate that in the code
I would recommend regexp to do the whole thing for ya 😃
This preg_match, finds... ; : , MD , M.D.
<?php $string = "bla bla bla, here is some text with , MD in it..."; if(preg_match('/(;|:|, MD|, M\.D\.)/s', $string)) { echo("Found it!"); } ?>