PHP explode string with tags using UTF8 between them

后端 未结 4 1789
梦谈多话
梦谈多话 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条回答
  •  日久生厌
    2021-01-28 09:38

    You can use strpos and Substr to do the same if your UTF is causing issues.

    This will loop till it can't find anymore heading and then add the last Substr after the loop.

    https://3v4l.org/UPfbb

    $content = "فهرست اولhi my name is mahdi  whats app فهرست دومhow are youفهرست اولhi my name is mahdi  whats app2 فهرست دومhow are you2";
    
    $oldpos =0;
    $pos =strpos($content, "",1); // offset 1 to exclude first heading.
    
    While($pos !== false){
        $arr[] = Substr($content, $oldpos, $pos-$oldpos);
        $oldpos = $pos;
        $pos =strpos($content, "",$oldpos+1); //offset previous position + 1 to make sure it does not catch the same again 
    }
    $arr[] = Substr($content, $oldpos); // add last one since it does not have a heading tag after itself.
    Var_dump($arr);
    

提交回复
热议问题