例子,PHP打印匹配行的自定义函数。
代码:
复制代码 代码示例:
function se_search_line($filename, $pattern)
{
if (is_readable($filename)) $lines = file($filename);
else return '';
$rs = array();
if (!emptyempty($lines))
{
foreach ($lines as $key => $value) {
if (preg_match($pattern, $value))
{
$rs[intval($key) + 1] = $value;
}
}
} // www.jb200.com
if ( !emptyempty($rs) )
{
$sp = PHP_SAPI === 'cli' ? "n" : "<br>";
foreach ($rs as $line => $value) {
//echo "#line: {$line}#{$sp}";
echo "{$value}";
}
}
}
// demo
se_search_line(__FILE__, "/lines/");