note 87977 added to function.strtok
| From: | Logikos at osu1 dot php dot net | Date: | Sun, 04 Jan 2009 06:12:02 +0000 |
| Subject: | note 87977 added to function.strtok | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148788@lists.php.net to get a copy of this message | ||
This looks very simple, but it took me a long time to figure out so I thought I'd share it
incase someone else was wanting the same thing:
this should work similar to substr() but with tokens instead!
<?php
/* subtok(string,chr,pos,len)
*
* chr = chr used to seperate tokens
* pos = starting postion
* len = length, if negative count back from right
*
* subtok('a.b.c.d.e','.',0) = 'a.b.c.d.e'
* subtok('a.b.c.d.e','.',0,2) = 'a.b'
* subtok('a.b.c.d.e','.',2,1) = 'c'
* subtok('a.b.c.d.e','.',2,-1) = 'c.d'
* subtok('a.b.c.d.e','.',-4) = 'b.c.d.e'
* subtok('a.b.c.d.e','.',-4,2) = 'b.c'
* subtok('a.b.c.d.e','.',-4,-1) = 'b.c.d'
*/
function subtok($string,$chr,$pos,$len = NULL) {
return implode($chr,array_slice(explode($chr,$string),$pos,$len));
}
?>
explode breaks the tokens up into an array, array slice alows you to pick then tokens you want, and
then implode converts it back to a string
although its far from a clone, this was inspired by mIRC's gettok() function
----
Server IP: 64.71.164.2
Probable Submitter: 76.193.203.6
----
Manual Page -- http://www.php.net/manual/en/function.strtok.php
Edit -- https://master.php.net/note/edit/87977
Del: integrated -- https://master.php.net/note/delete/87977/integrated
Del: useless -- https://master.php.net/note/delete/87977/useless
Del: bad code -- https://master.php.net/note/delete/87977/bad+code
Del: spam -- https://master.php.net/note/delete/87977/spam
Del: non-english -- https://master.php.net/note/delete/87977/non-english
Del: in docs -- https://master.php.net/note/delete/87977/in+docs
Del: other reasons-- https://master.php.net/note/delete/87977
Reject -- https://master.php.net/note/reject/87977
Search -- https://master.php.net/manage/user-notes.php