strtok() - 标记分割字符串 - php 字符串函数
strtok()
(PHP 4, PHP 5, PHP 7)
标记分割字符串
说明
strtok(string $str, string $token) : stringstrtok(string $token) : stringstrtok()将字符串$str分割为若干子字符串,每个子字符串以$token中的字符分割。这也就意味着,如果有个字符串是"This is an example string",你可以使用空格字符将这句话分割成独立的单词。
注意仅第一次调用 strtok 函数时使用 string 参数。后来每次调用 strtok,都将只使用 token 参数,因为它会记住它在字符串 string 中的位置。如果要重新开始分割一个新的字符串,你需要再次使用 string 来调用 strtok 函数,以便完成初始化工作。注意可以在 token 参数中使用多个字符。字符串将被该参数中任何一个字符分割。
参数
$str被分成若干子字符串的原始字符串。
$token分割$str时使用的分界字符。
返回值
标记后的字符串。
范例
Example #1strtok()范例
当存在空的部分时strtok()的反应
以上例程会输出:
string(9) "something" bool(false)
注释
Warning此函数可能返回布尔值FALSE
,但也可能返回等同于FALSE
的非布尔值。请阅读布尔类型章节以获取更多信息。应使用===运算符来测试此函数的返回值。
参见
- split() 用正则表达式将字符串分割到数组中
explode()
使用一个字符串分割另一个字符串
Result: Hello to all
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@maisuma you invert paramaters of explode() and strtok() functions, your code does not do what you expect. You expect to read the input string token after token so equivalent code for strtok() is arra_filter(explode()) because explode() return lines of empty string when several delimiters are contiguous in the read string, for example 2 whitespaces between. In fact strtok() is much faster (x2 at least) than arra_filter(explode()) if the read string contains several contiguous delimiters , it is slower if the read string contains one and only one delimiter between tokens.Note that strtok may receive different tokens each time. Therefore, if, for example, you wish to extract several words and then the rest of the sentence:Might be pointing out the obvious but if you'd rather use a for loop rather than a while (to keep the token strings on the same line for readability for example), it can be done. Added bonus, it doesn't put a $tok variable outside the loop itself either. Downside however is that you're not able to manually free up the memory used using the technique mentioned by elarlang.As of the change in strtok()'s handling of empty strings, it is now useless for scripts that rely on empty data to function. Take for instance, a standard header. (with UNIX newlines) http/1.0 200 OK\n Content-Type: text/html\n \n --HTML BODY HERE--- When parsing this with strtok, one would wait until it found an empty string to signal the end of the header. However, because strtok now skips empty segments, it is impossible to know when the header has ended. This should not be called `correct' behavior, it certainly is not. It has rendered strtok incapable of (properly) processing a very simple standard. This new functionality, however, does not affect Windows style headers. You would search for a line that only contains "\r" This, however, is not a justification for the change.Here is a java like StringTokenizer class using strtok function: * */ class StringTokenizer { /** * @var string */ private $token; /** * @var string */ private $delim; /** * Constructs a string tokenizer for the specified string * @param string $str String to tokenize * @param string $delim The set of delimiters (the characters that separate tokens) * specified at creation time, default to ' ' */ public function __construct(/*string*/ $str, /*string*/ $delim = ' ') { $this->token = strtok($str, $delim); $this->delim = $delim; } public function __destruct() { unset($this); } /** * Tests if there are more tokens available from this tokenizer's string. It * does not move the internal pointer in any way. To move the internal pointer * to the next element call nextToken() * @return boolean - true if has more tokens, false otherwise */ public function hasMoreTokens() { return ($this->token !== false); } /** * Returns the next token from this string tokenizer and advances the internal * pointer by one. * @return string - next element in the tokenized string */ public function nextToken() { $current = $this->token; $this->token = strtok($this->delim); return $current; } } ?>Here's a simple class that allows you to iterate through string tokens using a foreach loop.I found this useful for parsing user entered links in text fields. e.g. This is a link function parselink($link) { $bit1 = trim(strtok($link, '')); $html = ''.$bit1.''; return $html; // This is a link }Remove GET variables from the URL Outputs: div class idThis function takes a string and returns an array with words (delimited by spaces), also taking into account quotes, doublequotes, backticks and backslashes (for escaping stuff). So $string = "cp 'my file' to `Judy's file`"; var_dump(parse_cli($string)); would yield: array(4) { [0]=> string(2) "cp" [1]=> string(7) "my file" [2]=> string(5) "to" [3]=> string(11) "Judy's file" } Way it works, runs through the string character by character, for each character looking up the action to take, based on that character and its current $state. Actions can be (one or more of) adding the character/string to the current word, adding the word to the output array, and changing or (re)storing the state. For example a space will become part of the current 'word' (or 'token') if $state is 'doublequoted', but it will start a new token if $state was 'unquoted'. I was later told it's a "tokeniser using a finite state automaton". Who knew :-)
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)