 <p>There is no %2C in your string, so it just continually iterates till it reaches the end of the string then is trying to read more and failing because it is already at the end of the string. </p> <p>You should add something like:</p> <pre><code>if ($pos&gt;strlen($haystack)) { return false; } </code></pre> <p>Into your loop. </p> <p>Also $haystack[$pos] is only one character while $endDelimiter is more than one character so that will never match unless your $endDelimiter is only a single character. </p>