假设我在PHP文件中有以下部分:
/**
* @SomethingStart
*/
protected static $var1 = '1';
protected static $var2 = '2';
protected static $var3 = '3';
/**
* @SomethingEnd
*/
我试图找出如何首先解析@SomethingStart和@SomethingEnd之间的内容(不包括评论,然后是如何替换这两个标签之间的内容。
答案 0 :(得分:4)
您可以使用以下函数获取文件的内容:
file
http://www.php.net/manual/en/function.file.php
返回一行数组。然后你可以使用foreach,并将行内容与
匹配$switch = false;
$lines = file('filepath');
$string = '';
foreach($lines as $k => $v)
{
if(preg_match('/@(.*)End$/'. $v))
{
$switch = false;
break;
}
if($switch == true)
{
// do replacements, or anything you want with the following lines
// or add, or remove, even if you might have some problems with it
// for this you might not consider using foreach, instead you might
// try array_walk
}
if(preg_match('/@(.*)Start$/', $v))
{
$switch = true;
}
$string .= $v;
}
echo $string;
对于array_walk,请阅读此http://www.php.net/manual/en/function.array-walk.php
试试吧。