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
Related
MAC OSX creates some unwanted dot underscore files to store file information.
There are currently two files in a directory on my file system - Test.php and ._Test.php.
I need to write a preg_match to accept only first file and ignore the metadata file. For eg.
preg_match('/^(.+)\.php$/', $fileName, $matches);
But, here $matches returns the same output for both $fileName = Test.php and ._Test.php.
Is there anyway to modify the regex to ignore the metadata file and accept only Test.php?
Use a negative lookahead to assert that the string doesn't start with ._:
^(?!\._)(.+)\.php$
Demo
PHP sample
<?php
$tests[] = "Test.php";
$tests[] = "._Test.php";
$tests[] = ".Testing.php";
$tests[] = "_Testing2.php";
$tests[] = "._greajo.com";
$tests[] = "_.greajo.com";
foreach ($tests as $test) {
if (preg_match("/^(?!\._)(.+)\.php$/i", $test, $m)) {
echo "Valid file: ".$m[1].PHP_EOL;
}
else {
echo "Invalid file".PHP_EOL; // discard?
}
}
Demo
Result
Valid file: Test
Invalid file
Valid file: .Testing
Valid file: _Testing2
Invalid file
preg_match('/^[^\.].*\.php$/', $fileName, $matches);
See demo https://implode.io/evnXDC
I'm trying to make a function in PHP that can delete code within two tags from all .js file within one folder and all its subfolders. So far everything works except preg_replace(). This is my code:
<?php
deleteRealtimeTester('test');
function deleteRealtimeTester($folder_path)
{
foreach (glob($folder_path . '/*.js') as $file)
{
$string = file_get_contents($file);
$string = preg_replace('#//RealtimeTesterStart(.*?)//RealtimeTesterEnd#', 'test2', $string);
$file_open = fopen($file, 'wb');
fwrite($file_open, $string);
fclose($file_open);
}
$subfolders = array_filter(glob($folder_path . '/*'), 'is_dir');
if (sizeof($subfolders) > 0)
{
for ($i = 0; $i < sizeof($subfolders); $i++)
{
echo $subfolders[$i];
deleteRealtimeTester($subfolders[$i]);
}
}
else
{
return;
}
}
?>
As mentioned I want to delete everything inside these tags and the tags themselve:
//RealtimeTesterStart
//RealtimeTesterEnd
It is important that the tags contains the forward slashes and also that if a file contains multiple of these tags, only code from //RealtimeTesterStart to //RealtimeTesterEnd is deleted and not from //RealtimeTesterEnd to //RealtimeTesterStart.
I hope that someone can help me.
You could also change your regex to use the [\s\S] character set which can be used to match any character, including line breaks.
So have the following
preg_replace('#\/\/RealtimeTesterStart[\s\S]+\/\/RealtimeTesterEnd#', '', $string);
This would remove the contents of //RealtimeTesterStart to //RealtimeTesterEnd and the tags themselves.
I'm assuming that //RealtimeTesterStart, //RealtimeTesterEnd and the code in between are on different lines? In PCRE . does NOT match newlines. You need to use the s modifier ( and you don't need the () unless you need the captured text for the replacement):
#//RealtimeTesterStart.*?//RealtimeTesterEnd#s
Also, look at GLOB_ONLYDIR for glob instead of array_filter. Also, also, maybe file_put_contents instead of fopen etc.
Maybe something like:
foreach (glob($folder_path . '/*.js') as $file) {
$string = file_get_contents($file);
$string = preg_replace('#//RealtimeTesterStart.*?//RealtimeTesterEnd#s', 'test2', $string);
file_put_contents($file, $string);
}
foreach(glob($folder_path . '/*', GLOB_ONLYDIR) as $subfolder) {
deleteRealtimeTester($subfolder);
}
First excuse me for the bad english.
I am trying to build a php script to search multiple webpages from a .txt file for specific word.
More specific:
I have a .txt file where i have stored many urls (every url is on one line, so if i have 10 urls the file have 10 lines) and i want the script to check the webpage content of each url for a specific word. So if the word is found on the webpage the script will return ONLINE othewise will return DOWN.
I build the script but the problem is that it always return ONLINE even if the url from file doesn't have the specific word in it's webpage content.
<?php
$allads = file("phpelist.txt");
print("Checking urls: <br><br><br><strong>");
for($index = 0; $index <count($allads); $index++)
{
$allads[$index] = ereg_replace("\n", "", $allads[$index]);
$data = file_get_contents('$allads[$index]');
$regex = '/save/';
if (preg_match($regex, $data)) {
echo "$allads[$index]</strong>...ONLINE<br><strong>";
} else {
echo "$allads[$index]</strong>...DOWN<br><strong>";
}
}
print("</strong><br><br><br>I verified all urls from file!");
?
To search the particular webpage for a given string, I'd use stripos() (case-insensitive) or strpos() (case-sensitive) instead of regular expressions:
if( stripos(haystack, needle) !== FALSE ) {
//the webpage contains the word
}
An example:
$str = 'sky is blue';
$wordToSearchFor = 'sky';
if (strpos($str, $wordToSearchFor) !== false) {
echo 'true';
}
else {
echo 'Uh oh.';
}
Demo!
Although, programmitcally skimming through webpages isn't considered a good practice and shouldn't be done unless it's absolutely necessary.
UPDATE:
In your file_get_contents call you're doing:
$data = file_get_contents('$allads[$index]');
You're using single quotes, and the variable values do not get replaced. You'll have to use double quotes to have file_get_contents fetch the actual URL. Replace it with:
$data = file_get_contents("$allads[$index]");
Another thing I noticed is that you're using the deprecated ereg_replace() function in your code. See the red box? Relying on depreacted functions are highly discouraged.
Your code, after all the above corrections, should look like:
$allads = file("phpelist.txt");
print("Checking urls: <br><br><br><strong>");
for($index = 0; $index <count($allads); $index++)
{
$allads[$index] = str_replace("\n", "", $allads[$index]);
$data = file_get_contents("$allads[$index]");
$searchTerm = 'the';
if (stripos($data, $searchTerm) !== false) {
echo "$allads[$index]</strong>...ONLINE<br><strong>";
}
else
{
echo "$allads[$index]</strong>...DOWN<br><strong>";
}
}
print("</strong><br><br><br>I verified all urls from 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
$allfiles = glob($searchdir."*.txt");
$elist = array();
foreach($allfiles as $file){
$lines = array_merge($elist, file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
}
foreach ($lines as $existing){
// this echos a number // echo "<br />Existing".$existing."<br />";
if (preg_match("/\b".$searchforthis."\b/i", $existing)) {
echo "A match was found.";
continue;
} else {
echo "A match was not found.";
$nodupe ="y";
continue;
}
}
In the above I am attempting to check for a match in a directory of files and return a true or false for the next step.
It is obviously not working. I echoed the line in attempt to troubleshoot but, I get a number not the word on the line.
The file(s) being searched single column of words with only 100 lines each. There may be up to 5 of them in the directory.
I echoed the paths and other vars and all is correct. In just never finds a match.
I know I should learn mysql but, I need this to work.
I am also unsure about the continue or break. This routine resides in a for loop inside an if.
I want this to stop looking when a match was found.
Thanks for any guidance.
I added the file write part here in case I messed that up causing the problem. It writes a number to the file and does not append even when I bypass the append switch below and null the statement that does not..
/****Write the entry if no match******/
if ($nodupe != "y"){
if($append == "n"){
$name .= $searchforthis."\n";
file_put_contents($filepath.$writefile.".txt", $name, LOCK_EX);
}
else{
file_put_contents($filepath.$writefile.".txt", $name, FILE_APPEND | LOCK_EX);
}
}
Try this:
<?php
# variables
$sDir = __DIR__; # php 5.3, for php <5.3 use dirname(__FILE__);
$sFilePattern = '*.php';
$sSearch = 'printf';
# config
$sRegExp = '/\b'.$sSearch.'\b/i';
# code
foreach (glob($sDir . DIRECTORY_SEPARATOR . $sFilePattern) as $sFile){
foreach (file($sFile) as $nLineNumber => $sLine){
if (preg_match($sRegExp, $sLine) == 1){
printf('<br/>Word "%s" found in %s, line %d', $sSearch, $sFile, $nLineNumber);
} // if
} // foreach
} // foreach
It's literally the same thing that yours. Should show 2 occurences of 'printf'.
The /m modifier will search across lines, so you don't need to scan each line individually:
$search = 'whatever';
foreach (glob($dir . '/*.txt') as $file) {
if (preg_match('/^' . $search . '$/m', file_get_contents($file))) {
echo "$file contains $search\n";
break;
} else {
echo "$file does not contain $search\n";
}
}
Alternatively, if your word lists don't change very much, you'll be better off just making them into PHP arrays and include()'ing them straight into your script:
$list = array(
'word1',
'word2',
'word3',
// ...
);
And then you can just use in_array() to scan for the word.