我怎么说“如果字符串包含“x””在 PHP 中?
我有一个变量:
$testingAllDay = $event->when[0]->startTime;
如果它是“全天”,则该变量将是这种格式:
2011-06-30
如果它不是“全天”,它将是这种格式:
2011-07-08T12:00:00.000-05:00
我想做类似的事情:
if ($testingAllDay does not contain "T"){
$AllDay = 1;
} else {
$AllDay = 0;
}
我需要使用 strstr()
这里,还是有另一个函数可以做到这一点?谢谢!
I have a variable:
$testingAllDay = $event->when[0]->startTime;
This variable will be this format if it is "All Day":
2011-06-30
It will be this format if it is not "All Day":
2011-07-08T12:00:00.000-05:00
I'm wanting to do something like:
if ($testingAllDay does not contain "T"){
$AllDay = 1;
} else {
$AllDay = 0;
}
Do I need to use a strstr()
here, or is there another function that does this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一种选择是使用 strpos 来查看字符串中是否存在“T”字符,如下所示
:据说,在这种情况下使用 strlen 可能会更快/更有效(尽管毫无意义) ,根据您的示例,无时间字段将始终长度为 10 个字符。
例如:
One option is to use strpos to see if the 'T' character is present in the string as follows:
That said, it would probably be faster/more efficient (although no doubt meaninglessly so) to use strlen in this case, as according to your example, the time-free field will always be 10 characters long.
For example:
使用 strpos:
或 strstr
Use strpos:
or strstr
如果这些是唯一可能的情况,甚至 strlen() 也可以。
If those are the only possible cases, even strlen() will do.
不完全回答这个问题,但你可以用 strlen() 检查。
即“全天”长度为 10,任何超过该长度的长度都不是。
not exactly answer to the question, but you could check with strlen().
i.e. "All Day" length is 10, anything above that is not.
您要查找的函数是
strpos()
。以下是一个例子,甚至选取变量名称的措辞:The function you're looking for is
strpos()
. The following is an example picking up your wording for the variable names even:strstr 和 strpos 是两个可以完成您的要求的函数。
strstr 将查看字符串中是否存在子字符串,并且它将从第一次出现的字符串回显到其余部分。
而 strpos 将为您提供字符串第一次出现的位置。
strstr and strpos are two functions by which you can complete your requirement.
strstr will see if substring exists in string and it will echo from first occurrence of string to rest.
While strpos will give you position of first occurrence of the string.