preg_match_all表示字符串

时间:2013-08-21 04:39:27

标签: php string preg-match preg-match-all

我希望从此字符串中获取America/Chicago

Local Time Zone (America/Chicago (CDT) offset -18000 (Daylight))

并让其适用于其他时区,例如America/Los_AngelesAmerica/New_York等。我对prey_match_all并不是很好,如果有人可以指导我如何正确地学习它,那么这是我第三次需要使用它。

2 个答案:

答案 0 :(得分:2)

这是你的正则表达式的解决方案

<强>代码     

    $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]);

 ?>

和OUTPUT

America/Chicago

希望这对您有所帮助。

答案 1 :(得分:0)

我会使用正则表达式:\((\S+)

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
----------------------------------------------------------------------