【问题标题】:preg_match_all with windows / macOS9 line endingspreg_match_all 带有 windows / macOS9 行尾
【发布时间】:2015-03-02 23:18:47
【问题描述】:

我有以下代码来创建一个有点像 doxygen 的代码

public function parserAction () {

    $code = file_get_contents('/www/htdocs/rumpho/application/controllers/parserStr.php');

    preg_match_all ( '/(private|public)?\s*function\s*(.*?)\s*\((.*?)\)\s*{(?:\n|\t|$)*(?:\/\*\*((?:\n|.|\t)*?)\*\/)?/msi', $code, $matches );

    var_dump( $matches );

    die;

}

当 parserStr.php 的行尾用 unix 编码时有效,但如果行尾用 Windows 或 MacOS9 编码则无效。

想法?

谢谢

【问题讨论】:

    标签: php preg-match line-breaks


    【解决方案1】:

    您想要的特殊字符是 \R,它与 \n\r\r\n 匹配。然后你的正则表达式变成:

    preg_match_all ( '/(private|public)?\s*function\s*(.*?)\s*\((.*?)\)\s*{(?:\R|\t|$)*(?:\/\*\*((?:\R|.|\t)*?)\*\/)?/msi', $code, $matches );
    //                                                                 here __^^              and __^^
    

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 2023-03-14
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2016-04-09
      • 1970-01-01
      相关资源
      最近更新 更多