Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6625161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:44:26+00:00 2026-05-25T21:44:26+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:44:27+00:00Added an answer on May 25, 2026 at 9:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I came across this bit of php code: <?php $long_url = http://example.com; $bit_ly
I came across this primer on coding HTML: http://css-tricks.com/examples/CleanCode/Beautiful-HTML.png . Among other things it
I was going through a website I've taken over and came across this section
I came across this (really) simple program a while ago. It just outputs the
I've recently been working with a simple Twitter API for PHP and came across
I was going through operator precedence section of php.net and came across this example
I'm trying to make a simple page transition effect. I came across this one
I am learning JSF and came across this line: <h:messages layout=table></h:messages> in a sample
Came across this error today. Wondering if anyone can tell me what it means:
I came across this article written by Andrei Alexandrescu and Petru Marginean many years

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.