简体   繁体   中英

division string using regular expressions

I would like to split a string like in Wordpress tag <!--nextpage-->. The only difference is that my tag will contain the title of the current page. eg. <!--my title of next page-->

I have this code:

$str = 'lorem<!--title1-->ipsum<!--title2-->dolor<!--title3-->sit<!--title4-->amet<!--title5-->consectetur';
$res = preg_split('/<\!--(.*?)-->/', $str, null, PREG_SPLIT_DELIM_CAPTURE);

which returns:

Array
(
    [0] => lorem
    [1] => title1
    [2] => ipsum
    [3] => title2
    [4] => dolor
    [5] => title3
    [6] => sit
    [7] => title4
    [8] => amet
    [9] => title5
    [10] => consectetur
)

My aim is:

Array
(
    [0] => Array
        (
            [0] => lorem
        )    
    [1] => Array
        (
            [0] => title1
            [1] => ipsum
        )    
    [2] => Array
        (
            [0] => title2
            [1] => dolor
        )    
    [3] => Array
        (
            [0] => title3
            [1] => sit
        )    
    [4] => Array
        (
            [0] => title4
            [1] => amet
        )    
    [5] => Array
        (
            [0] => title5
            [1] => consectetur
        )    
)

You can use preg_match_all with the following regex:

(?:<\!--(.*?)-->)?(.+?)(?=<!--|$)

See demo

PHP sample code:

<?php
   $re = "/(?:<\\!--(.*?)-->)?(.+?)(?=<!--|$)/"; 
   $str = "lorem<!--title1-->ipsum<!--title2-->dolor<!--title3-->sit<!--title4-->amet<!--title5-->consectetur"; 
   preg_match_all($re, $str, $matches);
   $arr = array();
   $first = false;
   for ($i = 0; $i < count($matches[0]); $i++) {
       if ($first) {
          array_push($arr, array($matches[1][$i],$matches[2][$i] ));
        }
      else {
         $first = true;
         array_push($arr, array($matches[2][$i]));
      }
   }
   print_r($arr);
?>

Results are:

Array                                                                                                                                                                
(                                                                                                                                                                    
    [0] => Array                                                                                                                                                     
        (                                                                                                                                                            
            [0] => lorem                                                                                                                                             
        )                                                                                                                                                            

    [1] => Array                                                                                                                                                     
        (                                                                                                                                                            
            [0] => title1                                                                                                                                            
            [1] => ipsum                                                                                                                                             
        )                                                                                                                                                            

    [2] => Array                                                                                                                                                     
        (                                                                                                                                                            
            [0] => title2                                                                                                                                            
            [1] => dolor                                                                                                                                             
        )                                                                                                                                                            

    [3] => Array                                                                                                                                                     
        (                                                                                                                                                            
            [0] => title3                                                                                                                                            
            [1] => sit                                                                                                                                               
        )                                                                                                                                                            

    [4] => Array                                                                                                                                                     
        (                                                                                                                                                            
            [0] => title4                                                                                                                                            
            [1] => amet                                                                                                                                              
        )                                                                                                                                                            

    [5] => Array                                                                                                                                                     
        (                                                                                                                                                            
            [0] => title5                                                                                                                                            
            [1] => consectetur                                                                                                                                       
        )                                                                                                                                                            
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM