PHP Regex to match multiline

家住魔仙堡 提交于 2019-12-20 07:22:27

问题


i need to build a pattern to match a multiline code

    $data['infos'] = array();
    foreach ($infos as $info) {
        $data['infos'][] = array(
            'title' => $special['title'],
            'text'  => $special['summary'],,
        );
    }
    $data['next'] = $this->url('page/next');
    $data['page'] = $this->url('page/curent');

my attempt was to use (\$data\['infos']\[] = array\(\n).*\n\t.*\n\t*\)\; but it did not work, but when i tried (\$data\['infos']\[] = array\() only this part was match

$data['infos'][] = array(

how do i build the pattern to match this part only

        $data['infos'][] = array(
            'title' => $special['title'],
            'text'  => $special['summary'],,
        );

also i love to see it simple so that i can modify it for other similar case.have been on it for a whole day with no luck, kindly help me out.

Thanks in advance.


回答1:


If a simple pattern would be sufficient depends on how your input could look like.

$re = '~\Q$data[\'infos\'][]\E.*?\);~s';
  • \Q...\E is used to match literally (also could escape the brackets/dollar).
  • .*? in single line mode (s flag) matches lazily any amount of any character.

See demo at regex101 or php demo at eval.in



来源:https://stackoverflow.com/questions/41523411/php-regex-to-match-multiline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!