开发者

preg_match_all with surrounding data

I have a large amount of log data that I need to get some relevant information out of. The syntax is:

WORD1 some text KEYWORD some text WORD2 WORD1 some text KEYWORD some text WORD2 WORD1 some text KEYWORD some text WORD2 WORD1 some text KEYWORD some text WORD2 

Would it be possible to use regex to get a certain block out of it - so that when I queried some keyword it would return WORD1 some text THIS_KEYWORD some text开发者_如何学编程 WORD2

PS: There could be multiple instances of one keyword, it should return all of the blocks (preg_match_all, PREG_SET_ORDER?).


As per you comment there will always be starting and finishing word for each block.

You can do the following:

  • Split the input string into blocks (array). An end of the block is identified by the whitespace which is followed by WORD2 and preceded by WORD1.
  • Search the array we got above for the keyword.

.

$arr = preg_split('/(?<=WORD2)\s+(?=WORD1)/',$log);
$keyword = 'KEYWORD1';
$result = preg_grep("/\b$keyword\b/",$arr);

Ideone Link


use UNGREEDY modifier

$found = preg_match_all("/WORD1.*KEYWORD.*WORD2/U",$x,$match);
if($found) print_r($match[0]);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜