Iterating over matches from preg_match_all

后端 未结 2 1658
北荒
北荒 2020-12-21 09:52

I am trying to figure out the mechanics of this plugin in WordPress. I have a preg_match_all function that looks like this:

preg_match_all(\'/(?<=\\\\[\\\         


        
2条回答
  •  被撕碎了的回忆
    2020-12-21 10:28

    If you do want to rewrite this code, then I suggest you look into PREG_SET_ORDER as last argument, instead of PREG_PATTERN_ORDER. This groups the result array by results first, and with match groups in the second level.

    Then you can just loop over it as follows:

    foreach ($matches as $matchgroup) {
        $postslug = $matchgroup[0];
    }
    

    You still need the [0] to get the "complete match". If your pattern had any (..) groups then [1] and [2] would correspond to those..

提交回复
热议问题