<?php
$file = 'C:\wamp\www\Killboard\EPChernarus1\PhitLog.txt';
$searchfor = 'Chernarus';
header('Content-Type: text/html');
$contents = file_get_contents($file);
$contents = str_replace("(ArmA-AH.net)", "(DayZNorway.com)", $contents);
$pattern = preg_quote($searchfor, '/');
$contents = str_replace("DayZ Instance: 11", " Map: Chernarus ", $contents);
$pattern = "/^.*$pattern.*$/m";
$contents = str_replace("PKILL", "Player Killed", $contents);
$contents = str_replace("CLOG", "Combat Logged", $contents);
if(preg_match_all($pattern, $contents, $matches)){
echo "<strong>";
echo "<div style ='font:11px/21px Arial,tahoma,sans-serif;color:#2983CB'>Killboard Epoch Chernarus: <br>";
echo '', implode(" <br>", $matches[0]);
echo "</strong>";
}
else
{
echo "No kills yet. Looks like everyone is playing nice.";
}
?>
After much help on here, code now looks this ^
Now i am trying to include the code below.
So that it will rename the weapon classes to more userfriendly names.
I have included the two .php files its looking for, but i am unsure to where i place it and if it will even run like it is, wich i doubt.
Could someone
if ($line_type == 'kill') {
include("killfeed_weapon_classnames.php");
include("killfeed_weapon_cleannames.php");
$swap_key = array_search($line_varlist['weapon'], $wcn);
if($swap_key != false) { $line_varlist['weapon'] = $wn[$swap_key]; }
}
Before all of your preg matching and right after $contents use:
$contents = str_replace("(Arma-AH.net)", "(DayZNorway.com)", $contents);
Edited forgot the parenthesis and next question. I am not going to just give you the next part, but look at the example below and follow along this should help you achieve what you want for that next question.
$myArray = array("firstName" =>"sam", "secondName" => "billy", "thirdName" => "sally");
$nameKey = array_search("billy", $myArray);
echo $nameKey;
if($nameKey){
$myArray[$nameKey] = "Tom";
}
print_r($myArray);
<?php
$file = 'C:\wamp\www\Killboard\EPChernarus1\PhitLog.txt';
$searchfor = 'Chernarus';
header('Content-Type: text/html');
$contents = file_get_contents($file);
$contents = str_replace("(ArmA-AH.net)", "(DayZNorway.com)", $contents);
$pattern = preg_quote($searchfor, '/');
$contents = str_replace("DayZ Instance: 11", " Map: Chernarus ", $contents);
$pattern = "/^.*$pattern.*$/m";
$contents = str_replace("PKILL", "Player Killed", $contents);
$contents = str_replace("CLOG", "Combat Logged", $contents);
if(preg_match_all($pattern, $contents, $matches)){
echo "<strong>";
echo "<div style ='font:11px/21px Arial,tahoma,sans-serif;color:#2983CB'>Killboard Epoch Chernarus: <br>";
echo '', implode(" <br>", $matches[0]);
echo "</strong>";
}
else
{
echo "No kills yet. Looks like everyone is playing nice.";
}
?>
This is what i ended up doing, now its hardly pretty, but it works.
Now i need to get the results to show in descending order.
Any chance someone could help me with this?
Related
I have the following code in PHP, which searches a file for lines containing a string:
<?php
echo "Results for: ";
echo ($_POST['query']);
echo "<br><br>";
$file = 'completed.db';
$searchfor = ($_POST['query']);
header('Content-Type: text/plain');
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
?>
In the line, echo implode("\n", $matches[0]); in echos an array, separated by spaces. If I wanted to separate the items by a different string, say $entry = '<br>', how would you do it?
For example, if $matches was
one
two
three
Then, the command should echo:
one<br>two<br>three<br>
Just add the break tag to the string
echo implode("\n<br>", $matches[0]);
And one more to trail since implode only goes between.
echo "<br>";
The following code simply uses implode with a different first argument, bu also adds the separator to the end (as per specification)
$entry = '<br>';
echo implode($entry, $matches[0]).$entry;
So what I want to do is print out all the strings in an array, but seperate them with <p> tags.
I tried this:
echo filter_var( implode( "<p>", $matches[0]), FILTER_SANITIZE_SPECIAL_CHARS); echo "</p>";
but it results in
string1</p>string2</p>string3</p>
instead of
<p>string1</p> <p>string2</p> <p>string3</p>
Any thoughts?
EDIT:
if you need the actual full code, here you go:
<?php
$file = 'logfile.log';
$searchfor = $_POST['name'];
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo filter_var( implode( "<p>", $matches[0]), FILTER_SANITIZE_SPECIAL_CHARS);
}
echo "</p>";
else{
echo "No matches found";
}
?>
You should use htmlspecialchars() for any arbitrary string that you want to safely display on your page, not filter_var().
Here's the complete solution:
foreach ($matches[0] as $str) {
echo '<p>'.htmlspecialchars($str).'</p>';
}
Or, if you really want an (ugly) one-liner:
echo implode('', array_map(function ($str) { return '<p>'.htmlspecialchars($str).'</p>'; }, $matches[0]));
I am trying to search in a txt file using php. It is supposed to search the txt file and display results that it got from the file.
Here is my php code:
<?php
$file = 'my file.txt';
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else {
echo "No matches found";
}
You should use $file to read the file's contents into the $contents variable. You can use get_file_contents for that. Also, it might be useful to turn this into a function, so you can re-use it for other files and search strings:
function searchInFile($file, $searchFor) {
$contents = get_file_contents($file);
if ($contents === false) return array(); // no file, no match
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
preg_match_all($pattern, $contents, $matches));
return $matches[0];
}
$matches = searchInFile('my file.txt', 'concert');
if (count($matches)) {
echo "Found matches:\n" . implode("\n", $matches);
} else {
echo "No matches found";
}
<?php
$file = file_get_contents("http://www.mywebsite.net/folder/indexhtml.asp");
preg_match_all('~<td width="23%" height="23" align="right">(.*?)</td>~is',$file, $matches5);
print implode("\n", $matches5[0]); //This prints out (Page: 889) on website ?>
I am trying to remove brackets and the word “Page” with “Number” from string called matches5 variable
Everytime I use this code below its shows blank which leaves me very confused. Please help me someone as im new to PHP
Second code
<?php
$file = file_get_contents("http://www.mywebsite.net/folder/indexhtml.asp");
preg_match_all('~<td width="23%" height="23" align="right">(.*?)</td>~is',$file, $matches5);
$result = preg_replace('/(?<=^| ).(?=$| )/sm', '', $matches5);
print implode("\n", $result[0]);
?>
First example (bad):
$array = array("");
$replace = array("");
// Please put the maximum number this (Page: maximum)
for($i = 1; $i <= 1000; $i++)
{
array_push($array, "(Page: $i)");
// Put your replace string this or leave blank
array_push($replace, "I am replaced :)");
}
$file = 'I am (Page: 129) this is test.';
$new_string = str_replace($array, $replace, $file);
echo $new_string;
Second example (good).
$file = 'I am (Page: 129) this is test.';
$new_string = preg_replace('(Page: ([0-9]+))', "", $file);
$new_string = str_replace("()", "", $new_string);
echo $new_string;
i have a text (text.txt) file like this:
shir
beer
geer
deer
i have also a php page with that source:
<?php
foreach (glob("*.txt") as $filename) {
$file = $filename;
$contents = file($file);
$reverse = array_reverse($file, true);
$string = implode("<br>" , $contents);
echo $string;
echo "<br></br>";
}
?>
I want that in the php page it will show:
deer
geer
beer
shir
from the end of the file to the beginning.
thank you
Looks like you are reversing the file name and not the contents.
Do
$reverse = array_reverse($content); // you can drop 2nd arg.
$string = implode("<br>" , $reverse);
in place of
$reverse = array_reverse($file, true);
$string = implode("<br>" , $contents);
Also you can remove the temp variables from you script and do:
foreach (glob("*.txt") as $filename) {
echo implode("<br>" , array_reverse(file($filename))) . "<br></br>";
}
<?php
foreach (glob("*.txt") as $filename) {
$file = $filename;
$contents = file($file);
$reverse = array_reverse($contents, true);
$string = implode("<br>" , $reverse);
echo $string;
echo "<br></br>";
}
?>
Your result was a $contents, without reverse.