Is it possible to use file_get_contents() to search multiple files? - php

I am a newbie to php and I would like to create php search apps.
My Follow code
<?php
$html = file_get_contents("folder/one.html");
if (strpos($html, "searchtext") !== false) {
echo "found";
} else {
echo "not found";
}
?>
Is it possible to search for multiple files???
My hard code method
<?php
$url1 = "folder/one.html";
$html1 = file_get_contents($url1);
if (strpos($html1, "searchtext") !== false) {
echo "found it from folder/one.html";
}
$url2 = "folder/two.html";
$html2 = file_get_contents($url2);
if (strpos($html2, "searchtext") !== false) {
echo "found it from folder/two.html";
}
......loop
$url9999 = "folder/ninethousandninehundredninety-nine.html";
$html9999 = file_get_contents($url9999);
if (strpos($html9999, "searchtext") !== false) {
echo "found it from folder/ninethousandninehundredninety-nine.html";
}
?>
anyidea how to use file_get_contents() to search multiple files?
Thank you very much

Assuming that all the files are in the same directory, you can do something like this:
<?php
foreach ( glob("folder/*.html") as $filename) {
$html = file_get_contents($filename);
if (strpos($html, "searchtext") !== false) {
print_r("Result found in $filename \r\n");
}
}
?>
This is not a particularly efficient way of looking for text in a file, and it will probably fail if you're looking for anything that might be multi-byte encoded, but it will do the job.

Related

php add 1 line to existing index.php

I have some websites and I want to add a new code just after <?php
I wrote this code but not works:
<?php
error_reporting(0);
$dir = __DIR__;
$index = $dir.'/index.php';
if (is_file($index)) {
$content = file_get_contents($index);
if (strpos($content, 'validator') === false) {
str_replace('<?php', '<?php require_once \'path/validator.php\';', $content);
//Write the index:
$write = fopen($index,"w");
fwrite($write,$content);
fclose($write);
}
//Check again:
$content = file_get_contents($index);
if (strpos($content, 'validator') === true) {
echo "Line added successfuly";
unlink($dir.'/install.php');
} else {
echo "Line not added";
}
}
?>
How can I do this?
Thanks...
strpos() never returns true. It either returns a numeric index or false.
So change the second check to
if (strpos($content, 'validator') !== false)
By the way, you can use file_put_contents() to write the file in one step, just as you use file_get_contents() to read it.

PHP Search for a string in remote webpage

I am trying to write a script with PHP where it'll open up a text file ./urls.txt and check each domain for a specific word. I am really new to PHP.
Example:
Look for the word "Hello" in the following domains.
List:
Domain1.LTD
Domain2.LTD
Domain3.LTD
and just simply print out domain name + valid/invalid.
<?PHP
$link = "http://yahoo.com"; //not sure how to loop to read each line from a file.
$linkcontents = file_get_contents($link);
$needle = "Hello";
if (strpos($linkcontents, $needle) == false) {
echo "Valid";
} else {
echo "Invalid";
}
?>
$arrayOfLinks = array(
"http://example.com/file.txt",
"https://www.example-site-2.com/files/file.txt"
);
$needle = "Hello";
foreach($arrayOfLinks as $link){ // loop through the array
$linkcontents = file_get_contents($link);
if (stripos($linkcontents , $needle) !== false) { // stripos is case-insensitive search
// the needle exists in $linkcontents
// !== false instead of != false since stripos can return 0 meaning the needle is the first word of the contents
echo "Valid";
} else {
// the word does not exist in the given text
echo "Invalid";
}
}
First of all use CURL in favor of file_get_contents() because of security.
This would be a correct strpos example:
//your previous code
if (strpos($linkcontents, $needle) !== false) { // see the !==
echo "Valid"; //Needle found
} else {
echo "Invalid"; //Needle not found
}
For more complex crawling you could use some regexp instead of strpos.

Does strpos have an issue with a string that has html in it?

strpos is not finding >$2.99<. I have echoed $html right before the if statement, and I can find >$2.99< in it, but the result is not found.
$webpage = file_get_contents ($itunesurl);
$html = htmlspecialchars ($webpage);
echo $html;
if(strpos($html, '>$2.99<') !== FALSE) {
echo 'found';
}
else {
echo 'not found';
}
Search $webpage rather than $html, since the > and < characters get converted to entities by htmlspecialchars.
if(strpos($webpage, '>$2.99<') !== FALSE) {
echo 'found';
}
else {
echo 'not found';
}

PHP: problems searching text in a file

I think i was doing halfway good to get this to halfway work. Anyways the following code works to find it on the first line, but i have a script that creates each on an individual line.
Please revise or create a completely new version of the following to make it to search for the form data on every line.
$search = $_POST['search'];
$file = file("SLIST.txt");
foreach($file as $line)
{
$line = trim($line);
if($line == $search)
{
echo $search . " WAS found in the database";
}
else
{
echo $search . " was NOT found in the database";
}
}
by form i mean there is a search form on the previous page. This page is the page where it tells you whether the text put into the search form matches a line in the file (ex: Line 1: BOOT Line 2: Tree Search entry: Tree Echo msg: Tree WAS found in the database.)
It is currently not working like i intended.
If you only want to know if the search string was in the file, and don't care on which line, then strpos() (doc) with file_get_contents() might be for you like this:
$file = file_get_contents('SLIST.txt');
$search = $_POST['search'];
if (strpos($file,$search)){
echo $search . " WAS found in the database";
}
else
{
echo $search . " was NOT found in the database";
}
If you want to know the line, your solutions should work as well if you change the if($line == $search) with my strpos().
If the line has to be exactly the search query you are looking for, then your solution should work just fine
It isn't so clear. I suppose the following is what you wanted.
<?php
$search = $_POST['search'];
$file = file("SLIST.txt");
$found = false;
foreach($file as $line) {
$line = trim($line);
if($line == $search) {
$found = true;
break;
}
}
if ($found)
{
echo $search . " WAS found in the database";
}
else {
echo $search . " was NOT found in the database";
}
?>
Are you looking to find a single string in an entire file. i know you want to look on every line but this is more efficient.
Try this
$file = file_get_contents("SLIST.txt");
if(strpos($file, $search))
{
echo $search . " WAS found in the database";
}
else {
echo $search . " was NOT found in the database";
}
If you just want to read lines then do
if(strpos($line, $search))
{
echo "found";
}
else
{
echo "not found";
}

How to put URL in array and search through array for matching string?

I am trying to search through a URL for a matching string, but the below code snippet doesn't seem to work.
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file = file($url);
if (in_array($search,$file)) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
If you are just searching for an occurrence of a string on the page, you can use
$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
echo 'Success!';
} else {
echo 'Fail';
}
in_array() checks if an array member is equal to your needle.
It is improbable many websites will have a line which is equal to a only.
Also, is allow_url_fopen enabled?
That code will only find a line that has the exact $search string (likely including whitespace). If you're parsing HTML, check PHP's DOMDocument classes. Or, you can use a regex to pull what you need.
As #alex says, check is allow_url_fopen is enabled.
Also you can use strpos to search the string:
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file_content = file_get_contents($url);
if (strpos($file_content, $search) !== false) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>

Categories