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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:45:43+00:00 2026-06-08T19:45:43+00:00

Basically I want PHP to open a config file, search for a string and

  • 0

Basically I want PHP to open a config file, search for a string and replace what comes after
The problem that I have in the code that I have created is that it finds the string $db_pass = and is able to replace it… but then in the file there is an extra "password"); line… so i need it to be able to replace the entire line or either cut the rest to be able to delete it.

    $dbFile = 'dbconfig.php';
    $String = "\$db_pass =\"new_password\";\n";
    file_put_contents($dbFile, str_replace("\$db_pass =", $String,    file_get_contents($dbFile)));

dbconfig.php

    <?php
    // Database Constants
    db_pass = "hi";
    db_user = "hssi";
?>

my current script outputs like this
dbconfig.php

       <?php
    // Database Constants
    db_pass = "new_password";
"hi";
    db_user = "hssi";
?>
  • 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-06-08T19:45:45+00:00Added an answer on June 8, 2026 at 7:45 pm

    You want to use a regular expression to replace the whole line, rather than the beginning of the line. preg_replace() is the PHP function for find and replacing with regular expressions.

    Sample code to do what you’re looking for would be:

    $dbFile = 'dbconfig.php';
    $String = '$db_pass = "new_password";';
    file_put_contents(
        $dbFile,
        preg_replace('/\$db_pass = "(.*)";/', $String, file_get_contents($dbFile))
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have a single page on my site that I want any php
I have a problem with the following javascript code. Basically I want to run
Basically, I'm editing files that have include file names and I want a quick
I have php generating html and I want to create layers. Basically I want
Basically for a Facebook tab I have a PHP app and want Heroku to
I have a problem in PHP. I am using <input type=file> to get the
I'm building the basic framework for my website and I have a config.php file
In PHP, i want to open more that one window and each one of
basically I want php to decide which version of the site pieces to load
I am new with cron job in php,basically i want to send email to

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.