我怎么说“如果字符串包含“x””在 PHP 中?

发布于 2024-11-17 06:24:38 字数 431 浏览 1 评论 0原文

我有一个变量:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

微暖i 2024-11-24 06:24:38

一种选择是使用 strpos 来查看字符串中是否存在“T”字符,如下所示

if (strpos($testingAllDay, 'T') !== false) {
    // 'T' was present in $testingAllDay
}

:据说,在这种情况下使用 strlen 可能会更快/更有效(尽管毫无意义) ,根据您的示例,无时间字段将始终长度为 10 个字符。

例如:

if(strlen($testingAllDay) > 10) {
    // 'T' was present in $testingAllDay
}

One option is to use strpos to see if the 'T' character is present in the string as follows:

if (strpos($testingAllDay, 'T') !== false) {
    // 'T' was present in $testingAllDay
}

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:

if(strlen($testingAllDay) > 10) {
    // 'T' was present in $testingAllDay
}
奶气 2024-11-24 06:24:38

使用 strpos:

if (strpos($testingAllDay,"T")!==false){

或 strstr

if (!strstr($testingAllDay,"T")){

Use strpos:

if (strpos($testingAllDay,"T")!==false){

or strstr

if (!strstr($testingAllDay,"T")){
多孤肩上扛 2024-11-24 06:24:38
if (strpos($testingAllDay, 'T') !== FALSE){
   ...
}
if (strpos($testingAllDay, 'T') !== FALSE){
   ...
}
燕归巢 2024-11-24 06:24:38

如果这些是唯一可能的情况,甚至 strlen() 也可以。

If those are the only possible cases, even strlen() will do.

哽咽笑 2024-11-24 06:24:38

不完全回答这个问题,但你可以用 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.

向地狱狂奔 2024-11-24 06:24:38

您要查找的函数是 strpos()。以下是一个例子,甚至选取变量名称的措辞:

$testingAllDayTPosition = strpos($testingAllDay, 'T');

$testingAllDayDoesNotContainT = false === $testingAllDayTPosition;

if ($testingAllDayDoesNotContainT){
   $AllDay = 1;
   } else {
   $AllDay = 0;
}

The function you're looking for is strpos(). The following is an example picking up your wording for the variable names even:

$testingAllDayTPosition = strpos($testingAllDay, 'T');

$testingAllDayDoesNotContainT = false === $testingAllDayTPosition;

if ($testingAllDayDoesNotContainT){
   $AllDay = 1;
   } else {
   $AllDay = 0;
}
抠脚大汉 2024-11-24 06:24:38

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文