Php preg_匹配所有字符串
我想从这个字符串中得到Php preg_匹配所有字符串,php,string,preg-match,preg-match-all,Php,String,Preg Match,Preg Match All,我想从这个字符串中得到America/Chicago Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight)) 并让它在其他时区工作,如美国/洛杉矶,美国/纽约等。我不太擅长于prey\u match\u all,还有,如果有人能告诉我如何正确学习它的好教程,因为这是我第三次需要使用它。这是我的正则表达式的解决方案 代码 $in = "Local Time Zone (America/Chicago (CDT) o
America/Chicago
Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight))
并让它在其他时区工作,如
美国/洛杉矶
,美国/纽约
等。我不太擅长于prey\u match\u all
,还有,如果有人能告诉我如何正确学习它的好教程,因为这是我第三次需要使用它。这是我的正则表达式的解决方案
代码
$in = "Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight))";
preg_match_all('/\(([A-Za-z0-9_\W]+?)\\s/', $in, $out);
echo "<pre>";
print_r($out[1][0]);
?>
希望这一定会对您有所帮助。我会使用正则表达式:\((\S+)
说明:
$in = "Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight))";
preg_match_all('/\(([A-Za-z0-9_\W]+?)\\s/', $in, $out);
echo "<pre>";
print_r($out[1][0]);
?>
我相信包括我在内的许多人都在这里了解到,这对我很有用,如果我的答案对你有用,请不要忘记投票接受。我会很高兴。如果你明年6月以后在休斯敦,我会给你买一杯啤酒。谢谢!真是太棒了charm@FabianBuentello,非常感谢。它不适用于美国/洛杉矶这样的时区代码>因为下划线在字符类中不匹配。@M42,请检查我的更新答案。谢谢你的建议。
preg_match_all('/\((\S+)/', $in, $out);
The regular expression:
(?-imsx:\((\S+))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
\( '('
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------