开发者

Possible to use PEAR SearchReplace to replace text within a .php file?

I came across this simple pear tutorial over here: http://www.codediesel.com/php/search-replace-in-files-using-php/

include 'File/Se开发者_如何学编程archReplace.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?


If you only need to adapt a single file, then do it manually:

$src = file_get_contents($fn = "script.php");
$src = str_replace('"%VARYINGTEXT%"', '"$currentYear/$currentPromotion"', $src);
file_put_contents($fn, $src);

str_replace is sufficient for your case.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜