[英]PHP return selected array value from preg_match() function
I am trying to get a return value for the day that is matched by the preg_match() function against an associative array to return the selected weekday. 我试图获取与preg_match()函数针对关联数组进行匹配以返回所选工作日的当天的返回值。 The code excerpt is below:
代码摘录如下:
$daysArray = array(
"Monday" => "Mon",
"Tuesday" => "Tues",
"Wednesday" => "Wed",
"Thursday" => "Thurs",
"Friday" => "Fri",
"Saturday" => "Sat",
"Sunday" => "Sun"
);
$weekdaysString = implode('|',$daysArray);
if (preg_match('/^\d{1,2}.\d{2}([ap])m\[(' . $weekdaysString . '|-).Only\]$/', $val)) {
echo "Match was found <br />";
} else if (preg_match('/^\d{1,2}.\d{2}([ap])m\[(' . $weekdaysString . '|-)-(' . $weekdaysString . '|-).Only\]$/', $val)) {
echo "Match was found 3<br />";
} else {
echo 'Not found ';
}
$val is '8:55pm[Tues-Thurs Only]'; $ val是“ 8:55 pm [仅限星期二]”;
Php has functions for that : PHP具有以下功能:
echo "Tomorrow is ".date("l", mktime(0, 0, 0, 4, 24, 2014));
//=> Tomorrow is Thursday
If what you want is to check if a string contains a weekday you can do this : 如果您要检查字符串是否包含工作日,则可以执行以下操作:
$daysArray = array(
"Monday" => "Mon",
"Tuesday" => "Tues",
"Wednesday" => "Wed",
"Thursday" => "Thurs",
"Friday" => "Fri",
"Saturday" => "Sat",
"Sunday" => "Sun"
);
$val = "The sun is warm!";
foreach($daysArray as $fullDay => $shortDay){
if(strpos(strtolower($val), strtolower($shortDay)){
echo "match on $fullDay!";
}
}
If you'd take the time to read the documentation on preg_match and preg_match_all you'll find that it is able to find matches and not only returns a boolean value. 如果您花时间阅读有关preg_match和preg_match_all的文档,您会发现它能够找到匹配项,并且不仅返回布尔值。
Using this new knowledge, you can use the matches to find your weekdays in the string you evaluated. 利用这一新知识,您可以使用匹配项在您评估的字符串中找到工作日。
Try this: 尝试这个:
$matches = array();
if (preg_match_all('/^\d{1,2}.\d{2}([ap])m\[(' . $weekdaysString . '|-).Only\]$/', $val, $matches)) {
echo "Match was found <br />";
// check the array $matches here
} //...
This code gives the last day name. 该代码提供了最后一天的名称。 This means that if
$val
contains only one day, this day will be displayed. 这意味着如果
$val
仅包含一天,则将显示这一天。 You can easily change this behavior to get only a result when $val
contains two days by removing the ?
您可以通过删除
?
轻松更改此行为,以在$val
包含两天时仅获得结果?
. 。 If you want to deal with more days, replace
?
如果您想处理更多的日子,请更换
?
with *
. 与
*
。
I think it is more useful to flip the associative array, to get the day name at the end and to use array_keys
to build the day subpattern ( $dayPat
). 我认为翻转关联数组,在末尾获取日期名称并使用
array_keys
来构建日期子模式( $dayPat
) $dayPat
。
$val = '8:55pm[Tues-Thurs Only]';
$daysCorr = array(
'Mon' => 'Monday',
'Tues' => 'Tuesday',
'Wed' => 'Wednesday',
'Thurs' => 'Thursday',
'Fri' => 'Friday',
'Sat' => 'Saturday',
'Sun' => 'Sunday');
$dayPat = implode('|', array_keys($daysCorr));
$pattern = <<<EOD
~
^
\d{1,2} : \d{2} [ap]m
\[
(?:
(?: $dayPat ) # first day
(?: - | [ ]&[ ] ) # day delimiter
)? # ? makes the first day optional, remove it if not needed
(?<day> $dayPat )
[ ] # Since the x modifier is used (free space mode),
# you must use [ ] to write a literal space
Only ]
\z # end of the string
~x
EOD;
if (preg_match($pattern, $val, $match))
echo $daysCorr[$match['day']];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.