开发者

Finding all characters till it finds the FIRST occurrence of a string given and then keep fetching the rest with preg_match_all

开发者 https://www.devze.com 2023-04-08 16:42 出处:网络
$HTML = \' <div class=\"tsts\">CONTENT GOES HERE 1ENDENDENDENDEND<div class=\"tsts\">CONTENT GOES HERE 2ENDENDENDENDEND <div class=\"tsts\">CONTENT GOES HERE 3ENDENDENDENDEND
$HTML = '

<div class="tsts">CONTENT GOES HERE 1ENDENDENDENDEND<div class="tsts">CONTENT GOES HERE 2ENDENDENDENDEND <div class="tsts">CONTENT GOES HERE 3ENDENDENDENDEND
<div class="tsts">CONTENT GOES HERE 4ENDENDENDENDEND

';

preg_match_all('%<div class="tsts">([\S\s]+)ENDENDENDENDEND%',$HTML,$matches);

I want it to find "CONTENT GOES HERE 1", "CONTENT GOES HERE 2", "CONTENT GOES HERE 3", "CONTENT GOES HERE 4" in the matches

By the way I MUST use \s\S because I need to match all kind of characters including special characters, new lines, tabs , but I want it to stop when it finds the ENDENDENDENDEND and get the other results

how do I do this? because its only stopping in the last occurence of ENDENDENDENDEND, and I want it to stop in the first one that it finds. so it can 开发者_StackOverflow中文版match the rest.

How do I do this? I tried so much and nothing =X. Thanks very much in advance.


I don't have php environment, however the point is the express isn't it? see the grep test:

kent$  echo "'

<div class="tsts">CONTENT GOES HERE 1ENDENDENDENDEND<div class="tsts">CONTENT GOES HERE 2ENDENDENDENDEND <div class="tsts">CONTENT GOES HERE 3ENDENDENDENDEND
<div class="tsts">CONTENT GOES HERE 4ENDENDENDENDEND

'; 

"|grep -Po "(?<=<div class="tsts">)([\s\S]*?)(?=ENDENDENDENDEND)"     

CONTENT GOES HERE 1
CONTENT GOES HERE 2
CONTENT GOES HERE 3
CONTENT GOES HERE 4


No, you don't have to use \s\S for that. And the + quantifier is likely the second problem here.

The normal .*? will do, if you don't forget the /s flag.
In your case a [^<>]*? might be even better, because you don't want to match tag delimiters.

0

精彩评论

暂无评论...
验证码 换一张
取 消