Re: Split haystack at nth occurrence of needle?
| From: | Chris Boget | Date: | Wed, 22 Sep 2004 16:05:33 +0000 |
| Subject: | Re: Split haystack at nth occurrence of needle? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-197578@lists.php.net to get a copy of this message | ||
> I've been RTFMing for about an hour and I can't find a string function
> to split haystack 'one<br>two<br>three<br>four<br>five' at
> the nth
> occurrence of needle '<br>'. strpos gives me the position of the first
> needle, and strrpos gives me the position of the last needle. But I'm
> looking for the position of one of the in-between needles, so that I
> can use substr() to trim everything from there on. What very obvious
> function was I unable to find in the manual?
You could use explode().
<?
$string = 'one<br>two<br>three<br>four<br>five';
$nthPos = 4;
$tmpArr = explode( '<br>', $string );
$nthString = $tmpArr[($nthPos - 1)];
?>
Chris