I've finally come back to doing a script I was working on awhile back, where originally I was trying to use a regular expression to find the first two paragraph elements in a block of text, and trim everything that occurred after. With all the learning I've been doing since, I've come to the realization that I can do it much faster with a combination of strpos() and substr(), and I'm just wondering if this is the best syntax here, or if there's something I'm missing that could clean it up even further?
What I'm doing is trying to find the second occurence of </p> and strip everything after. So what I did was this:
PHP Code:
$pos = strpos($text, "</p>", strpos($text, "</p>")+4);
$newtext = substr($text, 0, $pos);
strpos() looks for the first occurrence after the specified offset which is why I use it inside of itself to find the first, then use that as the offset to find the second. It clocks in at 0.000061 seconds. The regular expression I was using,
PHP Code:
$newtext = preg_replace("~(.*?</p>.*?</p>).*~is", "$1", $text);
clocks in at 0.000095 seconds and seems to me like it could be written better too, but when put against this block of text,
PHP Code:
$text = <<<EOF
<p> this is a test 1 </p>
<p> this is a test 2 </p>
<p> this is a test 3 </p>
EOF;
it's the only thing I could get to work. Er, wait as I was writing this, I got "~((.+?</p>){2}).*~is" to work as well, yet again it seems like I should be able to write it cleaner.
Either way, strpos() still seems to be the fastest method. Any ideas on better ways?

-m