I have a php file that can read the contents of other files perfectly and return them as a string.
$contents = $file->read(); // return as string.
i need to be able to search and replace certain lines
lines that begin with $this->Session->setFlash
and end with , true));
must be replaced with
lines that begin with $this->Session->setFlash
and end with , true), 'default', array('class'=>'flash_failure'));
I have grep in my machine, if that helps.
finally after the contents is changed, i have a function that will write the contents back
$file->write($contents);
I know this helps to find the lines, but I have no idea how to replace.
^.*Session->setFlash.*, true\)\);$
You can try:
$contents = $file->read(); // return as string.
// change contents.
$contents = preg_replace('/^(\$this->Session->setFlash.*?), true\)\);$/',"$1, true), 'default', array('class'=>'flash_failure'));",$contents);
$file->write($contents);
the solution is thus:
$contents = preg_replace('/^(.*Session->setFlash.*, true\))\);$/m',
"$1, 'default', array('class'=>'flash_failure'));", $contents);
the multiline modifier is key.
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.