check a text is before or after another text
added comments for each step
Use preg_quote to convert special characters