可以使用 PEAR SearchReplace 替换 .php 文件中的文本吗?
我在这里遇到了这个简单的梨教程: http:// www.codediesel.com/php/search-replace-in-files-using-php/
include 'File/SearchReplace.php' ;
$files_to_search = array("fruits.txt") ;
$search_string = "apples";
$replace_string = "oranges";
$snr = new File_SearchReplace($search_string,
$replace_string,
$files_to_search,
'', // directorie(s) to search
false) ;
$snr->doSearch();
echo "The number of replaces done : " . $snr->getNumOccurences();
笔者以fruits.txt文件为例。
我想搜索并替换 .php 文件。
基本上我想要实现的目标是:
在用户交互中,打开index.php,搜索
$promoChange = "%VARYINGTEXT%";
并替换为
$promoChange = "$currentYear/$currentPromotion";
$current
变量会有所不同,因此只需更改 ""
之间的单词。
有人对如何完成此类任务有任何意见吗?
如果有人知道与该主题相关的任何教程,我们也将不胜感激。
谢谢你!
- 关于模板和用户交互,我确实已经弄清楚了其他所有事情,我只是在尝试找出如何完成这种类型的搜索和替换时遇到了麻烦。我知道应该如何完成它,因为我已经使用 Visual Basic 制作了类似的东西。但我开始认为我的答案是 perl?我希望事实并非如此...
好吧,我的问题部分解决了:
// Define result of Activate click
if (isset($_POST['action']) and $_POST['action'] == 'Activate')
{
include ''.$docRoot.'/includes/pear/SearchReplace.php' ;
$files = array( "$docRoot/promotions/index.php" ) ;
$snr = new File_SearchReplace( '$promoChange = "";', '$promoChange = "'.$currentYear.'/'.$currentPromotion.'";', $files) ;
$snr -> doSearch() ;
}
但是我如何让它搜索并替换类似 $promoChange = "%VARYINGTEXT%";
它发现并将“”替换为当前会话值。但现在情况已经改变,我需要它来替换“AND”之间的文本。
有人有什么想法吗?
I came across this simple pear tutorial over here: http://www.codediesel.com/php/search-replace-in-files-using-php/
include 'File/SearchReplace.php' ;
$files_to_search = array("fruits.txt") ;
$search_string = "apples";
$replace_string = "oranges";
$snr = new File_SearchReplace($search_string,
$replace_string,
$files_to_search,
'', // directorie(s) to search
false) ;
$snr->doSearch();
echo "The number of replaces done : " . $snr->getNumOccurences();
The writer uses the fruits.txt file as an example.
I would like to do a search and replace on a .php file.
Basically what I am trying to achieve would be this:
On a user interaction, index.php is opened,
$promoChange = "%VARYINGTEXT%";
is searched for and replaced with
$promoChange = "$currentYear/$currentPromotion";
The $current
variables will vary, hence the need to change the words inbetween the ""
only.
Does anyone have any input on how this type of task could be accomplished?
If anyone knows of any tutorials relating to this subject, that too would be greatly appreciated.
Thank you!
- I do have everything else figured out, regarding the template and user interaction, I am just having trouble trying to work out how to accomplish this type of search and replace. I have an understand of how it should be done as I have made something similiar using visual basic. But I am starting to this that my answer for this would be perl? I hope that this is not so...
Okay, my problem is partly solved with this:
// Define result of Activate click
if (isset($_POST['action']) and $_POST['action'] == 'Activate')
{
include ''.$docRoot.'/includes/pear/SearchReplace.php' ;
$files = array( "$docRoot/promotions/index.php" ) ;
$snr = new File_SearchReplace( '$promoChange = "";', '$promoChange = "'.$currentYear.'/'.$currentPromotion.'";', $files) ;
$snr -> doSearch() ;
}
but how do i get it to search and replace something like $promoChange = "%VARYINGTEXT%";
It found and replaced "" with the current session values. But now that is has changed, I need it to replace and text inbetween "AND".
Any ideas anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只需要调整单个文件,请手动执行:
str_replace
足以满足您的情况。If you only need to adapt a single file, then do it manually:
str_replace
is sufficient for your case.你到底为什么要做这样的事?像 PHP 这样的框架确实存在,其基础是不必为同一交互的每个不同视图编写页面。仅包含您现在想要更改的 PHP 页面并在调用之前相应地设置变量有什么问题吗?
Ontopic:纯粹从技术角度来说,我不明白为什么你所做的事情是一个问题。这可以使用 PHP 来完成。但实际上,你不应该这样做。
Why on earth do you want to do something like that? Frameworks like PHP do exist solely on the base of not having to write a page for each different view of the same interaction. What's wrong with just including the PHP page you now want to change, and set the variables accordingly before calling it?
Ontopic: I don't see why what you're doing is a problem, purely technically speaking. This can be done using PHP. But really, you shouldn't.