如何检查两个字符串是否相等?
how to check if two strings are equals?
我的代码需要一些帮助,我无法检查两个不同的字符串,因为我想检查变量 $program_times
中的字符串是否等于 PM
和 $next_program_times
等于 AM
。当我尝试时,它不会显示任何内容,也不会执行任何操作。
当我尝试这个时:
if(strpos($program_times, 'PM') !== false && strpos($next_program_times, 'AM') !== false)
{
echo 'hello 4';
}
我也试过这个:
if (strpos($next_program_times, 'AM') !== false)
{
echo 'hello 3';
}
完整代码如下:
for ($jj = 1; $jj <= 113; $jj++)
{
$program_title = $html->find("li[id=row$ii-$jj]", 0)->plaintext; // with this
$program_title = preg_split("/ {6,}/",$program_title);
$program_times = $program_title[1];
if (!empty($program_titles))
{
$program_times = (new Datetime($program_times))->add(new DateInterval('PT5H'))->format('g:i A');
echo '<span id="time', $show_id, '">', $program_times, '</span> ', '<span id="title', $show_id, '">', $program_titles, $program_bbf, $program_cat, '</span> <br></br>';
$next_program_times = $program_times + 1;
if (strpos($next_program_times, 'AM') !== false)
{
echo 'hello 3';
}
if(strpos($program_times, 'PM') !== false && strpos($next_program_times, 'AM') !== false)
{
echo 'hello 4';
}
这是输出:
10:00 PM Reba - To Tell the Truth
10:30 PM Reba - Brock's Mulligan
11:00 PM Job or No Job - Chicago Restaurants
12:00 AM Bruce Almighty
2:00 AM 17 Again
4:00 AM The 700 Club
5:00 AM Beetlejuice
7:00 AM Sexy in 3 Weeks!
7:30 AM Paid Programming
8:00 AM The 700 Club
你能告诉我一个例子,说明在我做某事之前如何检查 PM
和 AM
之间的字符串吗?
比较可以这样写:
if ($program_times == "PM")
还有 strpos returns 字符串第一次出现的位置而不是布尔值
您的问题出在代码的主要逻辑上。您正在从中提取:
12:00 AM Bruce Almighty
字符串的第一部分,您尝试检查它是否包含 'AM' 或 'PM'。由于您正在提取时间,因此您可以将其转换为日期 php 对象,而不是将其作为字符串处理。如果 $next_program_times
处于 AM 或 PM 时间段,您也可以通过这种方式获得。
当您尝试将 +1 添加到“12:00 AM”时,您不会得到“1:00 PM”!
所以我会像这样将字符串转换为日期对象:
$program_times = strtotime($program_times);
$refer_time = strtotime('01:00 PM');
if($program_times<$refer_time){
//we are in the AM range
}else{
//we are in the PM range
}
这假设您只考虑 24 小时范围,以便您评估的所有时间都指的是同一天。
对于 $next_program_times
我也会采用不同的方法:
$next_program_times = strtotime($program_times)+3600;
然后你可以用同样的方法再去一次:
if($next_program_times<$refer_time){
//we are in the AM range
}else{
//we are in the PM range
}
请注意,我不是很熟悉时间的子午线表示法 (AM/PM),所以可能我没有在 $refer_time
值中使用 PM 的第一个值。
您可以找到有关 php strtotime 函数的更多信息 here。
我的代码需要一些帮助,我无法检查两个不同的字符串,因为我想检查变量 $program_times
中的字符串是否等于 PM
和 $next_program_times
等于 AM
。当我尝试时,它不会显示任何内容,也不会执行任何操作。
当我尝试这个时:
if(strpos($program_times, 'PM') !== false && strpos($next_program_times, 'AM') !== false)
{
echo 'hello 4';
}
我也试过这个:
if (strpos($next_program_times, 'AM') !== false)
{
echo 'hello 3';
}
完整代码如下:
for ($jj = 1; $jj <= 113; $jj++)
{
$program_title = $html->find("li[id=row$ii-$jj]", 0)->plaintext; // with this
$program_title = preg_split("/ {6,}/",$program_title);
$program_times = $program_title[1];
if (!empty($program_titles))
{
$program_times = (new Datetime($program_times))->add(new DateInterval('PT5H'))->format('g:i A');
echo '<span id="time', $show_id, '">', $program_times, '</span> ', '<span id="title', $show_id, '">', $program_titles, $program_bbf, $program_cat, '</span> <br></br>';
$next_program_times = $program_times + 1;
if (strpos($next_program_times, 'AM') !== false)
{
echo 'hello 3';
}
if(strpos($program_times, 'PM') !== false && strpos($next_program_times, 'AM') !== false)
{
echo 'hello 4';
}
这是输出:
10:00 PM Reba - To Tell the Truth
10:30 PM Reba - Brock's Mulligan
11:00 PM Job or No Job - Chicago Restaurants
12:00 AM Bruce Almighty
2:00 AM 17 Again
4:00 AM The 700 Club
5:00 AM Beetlejuice
7:00 AM Sexy in 3 Weeks!
7:30 AM Paid Programming
8:00 AM The 700 Club
你能告诉我一个例子,说明在我做某事之前如何检查 PM
和 AM
之间的字符串吗?
比较可以这样写:
if ($program_times == "PM")
还有 strpos returns 字符串第一次出现的位置而不是布尔值
您的问题出在代码的主要逻辑上。您正在从中提取:
12:00 AM Bruce Almighty
字符串的第一部分,您尝试检查它是否包含 'AM' 或 'PM'。由于您正在提取时间,因此您可以将其转换为日期 php 对象,而不是将其作为字符串处理。如果 $next_program_times
处于 AM 或 PM 时间段,您也可以通过这种方式获得。
当您尝试将 +1 添加到“12:00 AM”时,您不会得到“1:00 PM”!
所以我会像这样将字符串转换为日期对象:
$program_times = strtotime($program_times);
$refer_time = strtotime('01:00 PM');
if($program_times<$refer_time){
//we are in the AM range
}else{
//we are in the PM range
}
这假设您只考虑 24 小时范围,以便您评估的所有时间都指的是同一天。
对于 $next_program_times
我也会采用不同的方法:
$next_program_times = strtotime($program_times)+3600;
然后你可以用同样的方法再去一次:
if($next_program_times<$refer_time){
//we are in the AM range
}else{
//we are in the PM range
}
请注意,我不是很熟悉时间的子午线表示法 (AM/PM),所以可能我没有在 $refer_time
值中使用 PM 的第一个值。
您可以找到有关 php strtotime 函数的更多信息 here。