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 3311992
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:56:30+00:00 2026-05-17T21:56:30+00:00

I am finding a line in a PHP file using PHP and writing to

  • 0

I am finding a line in a PHP file using PHP and writing to it by changing a variable at a particular line. Please see function below.

This works fine when I test it on its own. However, when I run it within my main script it doesn’t work properly. I find the following sort of thing on = "Version_3_18110"; in the PHP file when it should be $version = "Version_3_18110";

Can this function be affected by echos further up the main script?? The string passed in as $version is always what I need it to be, it just doesn’t get written correctly.

What is going on?

function edit_config_version($version){
    $version = trim($version);
    $file = fopen("../includes/db-connect.php", "r") or exit("Unable to open file!");
    $count = 0;
    while(!feof($file)){
            $line = fgets($file);
            if(substr($line, 0, 10)=='$version ='){
                    $line_number = $count;
            }
            $count++;
    }

    fclose($file);

    $count = 0;

    $file = fopen("../includes/db-connect.php", "r+") or exit("Unable to open file!");

    while(!feof($file)){
            if($line_number==$count){
                    fwrite($file, '$version = "Version_'.$version.'";'."\r\n");
            }
            $line = fgets($file);
            $count++;
    }
    fclose($file);
}

Contenets of db_connect.php:

/*
* Date: 06/10/09
* Last Updated: 06/04/2010
*/

$serverName = 'ABS-PC';

$monitor_name = "BTSH_Mon_3_18111";

$version = "Version_3_18112";

$full_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$full_url = explode('view-report.php', $full_url);
$sitePath = $full_url[0];

$full_url = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']).'/'; 
$sitePathFolder = $full_url;

/* 
* Make sure to close the connection in the scripts
* sqlsrv_close( $conn);
*/
  • 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-17T21:56:30+00:00Added an answer on May 17, 2026 at 9:56 pm

    Although this is a more exotic approach making use of PHPs tokenizer lib, I think this is better then error-prone string parsing. Try this if it works for you (tested unter PHP 5.3, but should work on any modern PHP version):

    <?php
    function edit_config_version($version){
        $version = trim($version);
    
        $source = file_get_contents('config.php');
        $tokens = token_get_all($source);
    
        $fh = fopen('config.php', 'w');
    
        foreach ($tokens as $token) {
            if(is_string($token)) {
                fwrite($fh, $token);
                continue;
            }
    
            list($id, $text) = $token;
    
            if($id == T_CONSTANT_ENCAPSED_STRING && strpos($text, 'Version_') === 1) fprintf($fh, '"Version_%s"', $version);
            else fwrite($fh, $text);
        }
    
        fclose($fh);
    }
    
    edit_config_version('2345_545454');
    

    Error handling is left as an exercise for the reader. 😉

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

Sidebar

Related Questions

Didn't got any luck finding an answer on google , so this is my
Hey, I wrote this (fortran) with the aim of finding the minimum spanning tree
I've seen this code for finding a Minor of a matrix: RegMatrix RegMatrix::Minor(const int
C:\xampp\htdocs contains Controller.php and ApplicationHelper.php . C:\xampp\htdocs\site contains index.php . Here is the error
I have a Subversion working copy with at least one missing file (the local
I just started reading The Dragon Book and I'm finding difficulty in understanding some
A question I haven't seen answered that I'm finding very interesting. All the other
[edit] I've removed my convoluted and badly malformed question so that it doesn't detract
I'd like to integrate Propel with Zend framework . I've seen the integration of
Can anyone instruct me on the proper way to use the MySQLi extension in

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.