preg_match with external txt file - php

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

Related

Search all files in directory PHP

I'm trying to search all of the files in a directory instead of one, but I don't know what to use to do that.
results.php:
<?php
// error_reporting(E_ERROR | E_PARSE);
$file = 'db/test.txt';
$searchfor = $_GET['q'];
if (!$_GET['q']) { // returns this if query is empty
echo "Search something.";
}
else {
// 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 "<centerResults</center><br>";
echo "<pre>";
echo implode($matches[0]);
echo "</pre>";
}
else
{
echo "<center>No results</center>";
}
}
I wanted to do something like this on line 3:
$file = 'db/*';
So that it could search through test.txt, and other txt files in that directory.
And i've tried googling, and nothing helpful has come up, any help?
Ther is function called glob it accept a required parameter which is the path and it can be a pattern of the files and it return an array of the paths of all the files that match that pattern .
in other words , you can use this function with "dir/*.txt" and it is going to return all the files that ar in the dir folder and has the txt extension

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];
}

Avoid Line Repeat in 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]);
}

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 />';
}
?>

Categories