PHP 中的字符串分词器
我最近遇到了一个奇怪的问题。
我有以下代码来标记 php 中的字符串:
$token = strtok($string, "#");
while ($token != false)
{
echo $token;
$token = strtok("#");
}
我遇到的简单问题是我正在解析包含许多数字的文件,因此在这种情况下 0 将被读取为 false。因此,解析无法完成。
我应该怎么办?
I've a strange problem that I faced recently.
I've the following code to tokenize string in php:
$token = strtok($string, "#");
while ($token != false)
{
echo $token;
$token = strtok("#");
}
The simple problem I've got is that I'm parsing file which contains many numbers, so in this case 0 will be read as false. So, parsing can't be completed.
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
!==
运算符,将$token
与false
进行比较:If you read the manual page of [**`strtok()`**][2], you'll see the following note *(quoting)* :
Using `!==` instead of `!=` will make sure there is no type-conversion done.
例如,
0 == false
;但是0 !== false
。You should use the
!==
operator, to compare$token
tofalse
:If you read the manual page of [**`strtok()`**][2], you'll see the following note *(quoting)* :
Using `!==` instead of `!=` will make sure there is no type-conversion done.
For instance,
0 == false
; but0 !== false
.