RJosh commented on a Page, PHPTok  -  Dec 06, 2009

It doesn't look like you have support for ranges or negative values.

IE:
gettok("hello there what's up",2-3," ") -> there what's
gettok("hello there what's up",-1," ") -> up

Maybe something you could look into. I had made a gettok that supported ranges a while back. i'll try and find it or remake it .

EDIT:

I took the liberty of doing my best to remake what i had and this is what i came up with.

function gettok($string,$n,$c) {
        if(is_numeric($c)) {
            $c = chr($c);
        }
        $tok = explode($c,$string);
        if($n==0) {
            return count($tok);
        }
        if($n < 0) {
            $n = count($tok)-$n;
        }
        if(@preg_match("#(\d+)-(\d+)?#",$n,$range)) {
            $text = "";
            if(count($range[1])==1) {
                $r1 = $range[1][0]-1;
                $r2 = count($tok);
            }   else    {
                $r1 = $range[1][0]-1;
                $r2 = $range[1][1];
            }
            while($r1<$r2) {
                $text .= $tok[$r1];
                $r1++;
            }
            return $text;
        }
        if(empty($tok[$n-1])) {
            return FALSE;
        }   else    {
            return $tok[$n-1];
        }
    }
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.