PHP explode string with tags using UTF8 between them

后端 未结 4 1828
梦谈多话
梦谈多话 2021-01-28 09:32

in php i want to explode string with tag using utf-8 between them, for example, in this text:

$content = \"فهرست اولhi my name is          


        
4条回答
  •  萌比男神i
    2021-01-28 09:40

    You can use preg_split to split the text by a regular expression, then array_filter to remove empty strings:

    $arr = array_filter(preg_split('/(?=.*?<\/heading>)/', $contents), 'strlen');
    

    It won't remove the tag, since it is in a look-ahead - a group construct that doesn't consume what it matched.

    For example:

    فهرست اولhi my name is mahdi  whats app فهرست دومhow are you
    

    This should return:

    array(
      [0] => "فهرست اولhi my name is mahdi  whats app ",
      [1] => "فهرست دومhow are you"
    )
    

    You can check this regex online: https://regex101.com/r/ITi7Lh/1
    Or, if you prefer, see how PHP parses it: (the link doesn't seem to work on SO, you have to manually paste it): https://en.functions-online.com/preg_split.html?command={"pattern":"\/(?=.*?<\\\/heading>)\/","subject":"\u0641\u0647\u0631\u0633\u062a \u0627\u0648\u0644<\/heading>hi my name is mahdi whats app \u0641\u0647\u0631\u0633\u062a \u062f\u0648\u0645<\/heading>how are you","limit":-1}

提交回复
热议问题