Lets say I have the following piece of start in a PHP file:
/**
* @SomethingStart
*/
protected static $var1 = '1';
protected static $var2 = '2';
protected static $var3 = '3';
/**
* @SomethingEnd
*/
I am trying to figure out how I can first parse out the content between the comments with @SomethingStart and @SomethingEnd (not including the comment and then secondly, how I can replace the content between those two tags.
You can get the contents of the file with the function:
file
http://www.php.net/manual/en/function.file.php
That returns an array of lines. Then you can use foreach, and match the line content with
$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;
For array_walk, read this http://www.php.net/manual/en/function.array-walk.php
Try it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.