Get string between - Find all occurrences PHP

前端 未结 6 1460
抹茶落季
抹茶落季 2020-12-08 16:37

I found this function which finds data between two strings of text, html or whatever.

How can it be changed so it will find all occurrences? Every data between every

6条回答
  •  醉话见心
    2020-12-08 17:06

    I needed to find all these occurences between specific first and last tag and change them somehow and get back changed string.

    So I added this small code to raina77ow approach after the function.

            $sample = 'One aaa TwoTwo Three Four aaaaa Five';
            $sample_temp = getContents($sample, '', '');
            $i = 1;
            foreach($sample_temp as $value) {
                $value2 = $value.'-'.$i; //there you can change the variable
                $sample=str_replace(''.$value.'',$value2,$sample);
                $i = ++$i;
            }
            echo $sample;
    

    Now output sample has deleted tags and all strings between them has added number like this:

    One-1 aaa TwoTwo-2 Three Four-3 aaaaa Five-4

    But you can do whatever else with them. Maybe could be helpful for someone.

提交回复
热议问题