Drastically decreasing runtime by lesser usage of preg_match, strlen and substr.