Re: [PHP] Split haystack at nth occurrence of needle?
* Thus wrote Curt Zirzow: > > $shortString = implode('', explode('', $string, $nth)); ignore this. Curt -- The above comments may offend you. flame at will. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Split haystack at nth occurrence of needle?
* 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 > >''. 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 = 'onetwothreefourfive'; > $nthPos = 4; > > $tmpArr = explode( '', $string ); > $newArr = array_slice($tmpArr,0,$nthPos-1); > $shortString = implode('',$newArr); and the short version: $shortString = implode('', explode('', $string, $nth)); Curt -- The above comments may offend you. flame at will. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Split haystack at nth occurrence of needle?
I don't understand why explode won't work for you. The explode solution is working. Thanks very much to everyone who replied with so much great information! - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Split haystack at nth occurrence of needle?
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 ''. 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, twothreefourfive'; $nthPos = 4; $tmpArr = explode( '', $string ); $newArr = array_slice($tmpArr,0,$nthPos-1); $shortString = implode('',$newArr); ?> A function that would give you the position to truncate from would be shorter. But, I don't seem to be finding that function either. So, the above seems like a solution. Janet - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Split haystack at nth occurrence of needle?
On Wed, 2004-09-22 at 12:31, Brian Dunning wrote: > > > $string = 'onetwothreefourfive'; > > $nthPos = 4; > > $tmpArr = explode( '', $string ); > > $nthString = $tmpArr[($nthPos - 1)]; > > ?> > > 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 > ''. So I'm looking for a function that will return the value 19, > given the above example string. The following should get you going: function strpos_nth( $hay, $needle, $n=1 ) { $offset = -1; if( $n < 1 ) { return false; } while( $n-- > 0 ) { if( ($offset = strpos( $hay, $needle, $offset + 1 )) === false ) { return false; } } return $offset; } Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Split haystack at nth occurrence of needle?
Hi if you want the *rest of the string from the nth *, 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: $string = "onetwothreefourfive"; $count = preg_match_all('/([^<]+)/', $string, $out, PREG_OFFSET_CAPTURE); 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 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" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> 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 'onetwothreefourfive' at the nth > occurrence of needle ''. 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 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Split haystack at nth occurrence of needle?
twothreefourfive'; $nthPos = 4; $tmpArr = explode( '', $string ); $nthString = $tmpArr[($nthPos - 1)]; ?> 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 ''. So I'm looking for a function that will return the value 19, given the above example string. - Brian -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [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 'onetwothreefourfive' at the nth > occurrence of needle ''. 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(). twothreefourfive'; $nthPos = 4; $tmpArr = explode( '', $string ); $nthString = $tmpArr[($nthPos - 1)]; ?> Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[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 'onetwothreefourfive' at the nth occurrence of needle ''. 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