使用PHP和preg_replace函数替换文件中的字符串 [英] Replace string in file using PHP and preg_replace function
本文介绍了使用PHP和preg_replace函数替换文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有一个txt文件,我想使用php进行一些更改
例如:
Hi i have a txt file and i want to use php to make some changes
example.:
var="Test need to change"
我想找到字符串测试并更改句子的其余部分
完成后应该是
I want to find string Test and change the rest of sentence
when finish it sould be
var="Test changed text".
我尝试过:
使用PHP和preg_replace函数替换文件中的字符串
What I have tried:
Replace string in file using PHP and preg_replace function
推荐答案
您需要使用后向引用在结果中包含匹配的字符串:
You need to use a back reference to include the matched string in the result:
<?php
string = ' 测试需要更改';
string = 'Test need to change';
pattern = ' /(试验)* / 跨度>。
pattern = '/(Test).*/';
这篇关于使用PHP和preg_replace函数替换文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文