[英]Write text between two text strings in PHP
Lets say I have this data in a file (externalfile.txt) 可以说我在文件中有这个数据(externalfile.txt)
#1#First#/1#
#2#Something#/2#
#end#
I want to write text between #/2#
and #end#
, How to do this? 我想在
#/2#
和#end#
之间写文字,怎么做?
Not sure if it's the proper way but you may take a look 不确定这是否正确,但你可以看一看
$newval='new text';
$file_contents = file_get_contents('externalfile.txt');
file_put_contents('externalfile.txt', preg_replace("/#\/2#/", "#/2#\n$newval\n", $file_contents));
It works, anyway. 无论如何它都有效。
This PHP code inserts a new text right before the last occurence of the #end#
string: 这个PHP代码在
#end#
string的最后一次出现之前插入一个新文本:
$str = file_get_contents('file.txt');
$splitted = explode('#end#',$str);
$pos = count($splitted)-2;
$splitted[$pos] .= 'new text';
$str = implode('#end#',$splitted);
echo $str;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.