$lines = file($filename);
$keep = false;
$keepTrailing = true; //Flag that decides wether to keep trailing capture segments or not.
$extractions = array();
$current = '';
foreach($lines as $line){
$parts = preg_split('/\bworld\b/i', $line);
$current .= $parts[0];
for ($i = 1; $i<count($parts); $i++){
if ($keep) $extractions[] = $current;
$keep = !$keep;
$current = $parts[$i];
}
}
if ($keep && $keepTrailing)
$extractions[] = $current;
var_dump($extractions);
Here it is in action.
基本上,通过文件迭代一次,就可以简单地拆分各行中的关键词(“世界”) - 我用\b
锚,以确保它没有对“世俗”或分拆其他垃圾。我已经添加了一个标志来决定是否保留尾随捕捉片段。你并不需要,但它可能会有所帮助。该解决方案唯一不直观的部分是将当前捕获保留在$current
变量中,这基本上允许您在多个换行符中继续扫描。
你知道,这很容易做成一个函数。
function capturingSearchWithKeyword($filename, $keyword, $keepTrailing = true, $trim = false){
$lines = file($filename);
$keep = false;
$extractions = array();
$current = '';
foreach($lines as $line){
$parts = preg_split("/\\b$keyword\\b/i", $line);
$current .= $parts[0];
for ($i = 1; $i<count($parts); $i++){
if ($keep){
if ($trim) $current = trim($current);
$extractions[] = $current;
}
$keep = !$keep;
$current = $parts[$i];
}
}
if ($keep && $keepTrailing)
$extractions[] = $current;
return $extractions
}
Check it out...
如果你告诉我们,你的正则表达式,我们可以尽力帮助 – Uby
由约瑟夫一世给出的答案试过,正则表达式,但没有结果 –
@Bhavin:你所说的“没有结果”是什么意思? - 约瑟夫包括它的工作示范。 – halfer