Avoid Line Repeat in PHP - php

I am working on a PHP code that reads data from text files and it searches for a certain word and echos it, for example, I search for
[Error]
Is it possible to only echo the word I search for only 1 time (aka if the word "Error" is found twice, only echo it once!)
$file = 'filesexample/'.$fileNameNew;
$searchfor = 'error';
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 "Errors Found:\n";
echo implode("\n", $matches[0]);
}
Can I do this?

You can use the array_unique() PHP's function which remove duplicate values from an array:
if(preg_match_all($pattern, $contents, $matches))
{
$matches[0] = array_unique($matches[0]);
echo "Errors Found:\n";
echo implode("\n", $matches[0]);
}

Related

Get text on next line of value found PHP

I have the following text file called people.txt with the contents:
mikey.mcgurk
Boss Man
michelle.mcgurk
Boss Man 2
I'd like to adjust my PHP script to grab the data on the line that follows each username, so if I was searching for mikey.mcgurk, my script would output Boss Man.
PHP:
<?php //
$file = 'people.txt';
$searchfor = "mikey.mcgurk";
// 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)){
// write all of this to a text file
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
you can do like this
$contents = file_get_contents($file);
$contents = explode(PHP_EOL, $contents);
if(array_search($searchfor, $contents) !== false){
echo $contents[array_search($searchfor, $contents)+1];
}
You can compare like this:
$contents = file_get_contents($file);
$lines = explode("\n",$contents);
for($i = 0; $i < count($lines); $i++) {
if( $lines[$i] ==$searchfor ) {
echo "Username ".$lines[$i+1];
}
}
Instead of using regular expression you can do this by getting all lines in an array
$lines = explode(PHP_EOL, $contents);
then get keys of results
$keys = array_keys($lines, $pattern);
and increment keys by 1 to get the next line
foreach ($keys as $key) {
echo $lines[++$key];
}

how to get errors and line number from php file without running?

i am trying to read line by line from a index.php file and want to display the error and line number of error.This is what i have tried
$code_to_check = "$name = 'soubhagya';echo $name";
$result = eval($code_to_check);
the above coding is not working because i put $ symbol in a variable
This is going to solve your problem
$file = 'file.php';
$searchforError = 'content';
header('Content-Type: text/plain');
$contents = file_get_contents($file);
$pattern = preg_quote($searchforError, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "Found error:\n";
echo implode("\n", $matches[0]);
$charpos = strpos($contents, implode("\n", $matches[0]));
list($before) = str_split($contents, $charpos);
$line_number = strlen($before) - strlen(str_replace("\n", "", $before)) + 1;
echo 'on line '.$line_number;
}
else{
echo "No errors found";
}

What would be the best way to search a txt file display all result on page in php

I have been trying to create a page where you can search a .txt file only using html and php.
Here is my code:
<?php
$file = 'myfile.txt';
if (preg_match_all($pattern, $contents, $matches)) {
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else {
echo "No matches found";
}
Please try this code. Your code is missing $contents
<?php
$file = 'somefile.txt';
$searchfor = 'search text would come here';
// get the file contents
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// regular expression
$pattern = "/^.*$pattern.*\$/m";
// search
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
Thanks
Amit

PHP find url in sa text and save in array

I have a txt file and I want to save in array all the urls starting from http:// and ends with .png or .jpg or .jpeg. How can I do that?
Can I use this?
<?php
function getUrls($string)
{
$regex = '/https?\:\/\/[^\" ]+/i';
preg_match_all($regex, $string, $matches);
return ($matches[0]);
}
$urls = getUrls($string);
foreach($urls as $url)
{
echo $url.'<br />';
}
?>
Is it correct?
The function you showed will not work because there's nothing specifying .png, .jpg, or .jpeg in the regex. It will also only return the first match: $matches[0]. Additionally, it needs /g flag to find multiple instances in the same string (/i just ignores case).
Try this instead:
<?php
function getUrls($string)
{
// \w is any word character
$regex = '/https?\:\/\/[\w]+\.(jpg|png|jpeg)/ig';
preg_match_all($regex, $string, $matches);
return ($matches);
}
$urls = getUrls($string);
foreach($urls as $url)
{
echo $url.'<br />';
}
?>

preg_match with external txt file

I have a .txt file that holds a lot of forbidden words in a forum, with the expression like:
//filterwords.txt
XXX
YYY
ZZZ
and then, I would like to use preg_match to check incoming text $str with these words; if those forbidden words are not included, we can do something; otherwise, we do another thing... I am not sure about the expression, and I just know:-
$filter_word = file("filterwords.txt")
for ($i=0; $i< count($filter_word);$i++)
{
if(!preg_match($filter_word[$i],$str))
{
echo "not ok!";
exit;
}
else
{
echo "ok!!";
exit;
}
}
Could experts teach me how to write the preg_match part? thankyou.
How about this:
<?php
$file = file_get_contents('filterwords.txt');
$words = preg_split("#\r?\n#", $file, -1, PREG_SPLIT_NO_EMPTY);
#Added to escape metacharacters as mentioned by #ridgerunner
$words = array_filter("preg_quote", $words);
$pattern = "#\b(". implode('|', $words) . ")\b#";
if(preg_match($pattern, $str))
{
echo "bad word detected";
}
?>
P.S. That's assuming that you have the text to check in the $str var

Categories