Search code examples
phpparsingpreg-match

parsing - Preg match text in php between wordpress shortcodes


Hello I would like to use preg_match in PHP to parse the nested Wordpress shortcodes out of the following from a string. but my preg doesn't work.

$str = '[according-wr]
            [according]
                [icon]eonrolf[/icon]
                [title]Title[/title]
                [src]http://dcijnionc.com/rfeojf[/src]
                [text]dsionodsovndsl lkdsnikndsln slodsvndx  sdvopndsponvdxs ndsxpn pd.[/text]
            [/according]
        [/according-wr]';

preg_match_all('/\[according-wr\](.*?)\[\/according-wr\]/', $str, $matches);

var_dump($matches);

it return an empty array!


Solution

  • The dot . is not matching new-line characters by default, you need the s modifier for that:

    $preg = preg_match_all('/\[according-wr\](.*?)\[\/according-wr\]/s', $str, $matches);
                                                                     ^ here
    

    Also see the manual for more information about the PCRE_DOTALL modifier.

    A working example.