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

  • SEARCH
  • Home
  • 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

This is the command I've been using for finding matches (queryString) in php files,
I'm opening a file and finding the line I need, but then I have
I am having trouble finding an answer to this. Consider the clipping code below:
I'm using Console_Getopt in PHP 5.2, and finding it surprising about how different it
I am using the following command to check for configuration file for PHP installation.
I have found that call stack helps in finding the line number of a
I'm having trouble finding a way to do this in a way that doesn't
I am new to spring MVC coming with experience using PHP MVC frameworks and
I am using php to pull information out of a database, but I have
Warning: Parameter 3 to showBlogSection() expected to be a reference, value given in /home/smartsta/public_html/includes/Cache/Lite/Function.php

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.