[PHP] Find & Edit After [PHP]

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello,

I'm trying to make a PHP installation file for one of my applications.
I have a text file like so:

inc21 : 12
inc22 : 29
inc87 : 21

I want PHP to search for the : and edit the "12" to a set string.
So if I have a string of text say "Hello", I want "12" to be replaced by "Hello".

How does one accomplish this.

I tried using this (see below) but it was returning the full line instead of just "12".

PHP:
<?php
$file = 'somefile.txt';
$searchfor = ': ';
 
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
 
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
  echo "Found matches:\n";
  echo implode("\n", $matches[0]);
}
else{
  echo "No matches found";
}
?>
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
, using this returns this:

2c47a3a63861660710c3f385a7a16cd2.png



File:
Code:
<?php
$file = fopen("somefile.txt", "r");
$members = array();
fclose($file);
foreach($members as $member){
    $value = array_shift( $members );
    $new = explode("inc21 : ", $value);
    $new2 = array_shift($new);
    $new3 = explode("inc22 : ", $new2);
    $new4 = array_shift($new3);
    $new5 = explode("inc87 : ", $new4);
    $value2 = array_shift( $new5 );
    echo $value2;
}
?>
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,135
2,461
It probably isn't really doing as you asked, my fault. But I don't get why you want to do this, what are you actually trying to do with this installation script?
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Well, I want to find what is after the : and replace it with the users selected configuration.
So, if I wanted inc21 to be "localhost", it will find whatever is after : and replace it with their selected host (localhost).
 

Users who are viewing this thread

Top