Re: Split haystack at nth occurrence of needle?

From: Date: Wed, 22 Sep 2004 16:38:35 +0000
Subject: Re: Split haystack at nth occurrence of needle?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-197585@lists.php.net to get a copy of this message
Hi if you want the *rest of the string from the nth <br>*, I think 'preg_match_all' with PREG_OFFSET_CAPTURE can help you (see http://www.php.net/manual/en/function.preg-match-all.php) You could do something like: <code> $string = "one<br>two<br>three<br>four<br>five"; $count = preg_match_all('/<br>([^<]+)/', $string, $out, PREG_OFFSET_CAPTURE); </code> and you will have an array of 2 arrays, where the first one contains the full pattern matches and the second one contains arrays for each string matched *and the starting offset* of that string. Therefore, to know where the 2nd <br> ends (so we would get 'three') all you have to do is: $out[1][1][1] => 14 Just my .2 euros... Silvio Porcellana ----- Original Message ----- From: "Brian Dunning" <brian@briandunning.com> To: <php-general@lists.php.net> Sent: Wednesday, September 22, 2004 5:54 PM Subject: [PHP] Split haystack at nth occurrence of needle? > 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? > > Thanks, > > - Brian > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#197585) next »