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".
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";
}
?>