Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中的两个文本字符串之间写入文本_Php_String - Fatal编程技术网

在PHP中的两个文本字符串之间写入文本

在PHP中的两个文本字符串之间写入文本,php,string,Php,String,假设我有一个文件(externalfile.txt)中的数据 我想在#/2#和#end#之间编写文本,如何执行此操作?此PHP代码在上次出现#end#字符串之前插入新文本: $str=file\u get\u contents('file.txt'); $splitted=explode('end#',$str); $pos=计数($splitted)-2; $splitted[$pos]。='新文本'; $str=内爆('end',$splitted); echo$str; 不确定这是不是正确

假设我有一个文件(externalfile.txt)中的数据


我想在
#/2#
#end#
之间编写文本,如何执行此操作?

此PHP代码在上次出现
#end#
字符串之前插入新文本:

$str=file\u get\u contents('file.txt');
$splitted=explode('end#',$str);
$pos=计数($splitted)-2;
$splitted[$pos]。='新文本';
$str=内爆('end',$splitted);
echo$str;

不确定这是不是正确的方法,但您可以看看

$newval='new text';
$file_contents = file_get_contents('externalfile.txt');
file_put_contents('externalfile.txt', preg_replace("/#\/2#/", "#/2#\n$newval\n", $file_contents));

无论如何,这是可行的。

我意识到这可能是由于语言障碍,但“请”和“谢谢”这两个词可能会让你走得更远。请告诉我如何做到这一点?谢谢,我有点困惑:-)
$newval='new text';
$file_contents = file_get_contents('externalfile.txt');
file_put_contents('externalfile.txt', preg_replace("/#\/2#/", "#/2#\n$newval\n", $file_contents));