Re: Split haystack at nth occurrence of needle?

From: Date: Wed, 22 Sep 2004 18:42:36 +0000
Subject: Re: Split haystack at nth occurrence of needle?
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-197595@lists.php.net to get a copy of this message
* Thus wrote Janet Valade: > Brian Dunning wrote: > > >Thanks Chris, that works great, but it's not doing what I want. I'm just > >trying to get the position of the 3rd occurrence (for example) of > >'<br>'. So I'm looking for a function that will return the value 19, > >given the above example string. > > From your first post, you just want to remove everything after a > certain occurrence. I don't understand why explode won't work for you. > As in, > > <? > $string = 'one<br>two<br>three<br>four<br>five'; > $nthPos = 4; > > $tmpArr = explode( '<br>', $string ); > $newArr = array_slice($tmpArr,0,$nthPos-1); > $shortString = implode('<br>',$newArr); and the short version: $shortString = implode('<br>', explode('<br>', $string, $nth)); Curt -- The above comments may offend you. flame at will.

« previous php.general (#197595) next »