php add 1 line to existing index.php - 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.

Related

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

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.

Read All Lines In A File And Remove Ones Containg Certain String

I'm rather new to PHP, and was trying to remove all lines where any instance of the string variable 'user' appears. My current code
if($action == "removeUser")
{
foreach(file('users.txt') as $line)
{
if (strpos($line, $parameters) !== false)
{
$line = "";
}
}
}
For some reason this doesn't seem to have any effect at all. What am I doing wrong?
You need to open the file and read the lines.
<?php
if($action == "removeUser")
{
$filename = "users.txt";
// Open your file
$handle = fopen($filename, "r");
$new_content='';
echo "Valid input: <br><br>";
// Loop through all the lines
while( $line = fgets($handle) )
{
//try to find the string 'user' - Case-insensitive
if(stristr($line,"user")===FALSE)
{
// To remove white spaces
$line=trim($line);
if($line!='') echo $line."<br>";
//if doesn't contain the string "user",
// add it to new input
$new_content.=$line."\n";
}
}
// closes the file
fclose($handle);
$new_content=trim($new_content); // Remove the \n from the last line
echo "<br>Updating file with new content...";
file_put_contents($filename,$new_content);
echo "Ok";
}
?>

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.

How to filter an input from file? (PHP)

First of all I am new to PHP world.. so it would be much appreciated if i found what i needed here. many thanks in advance.
I have an input file with multiple lines, each line has some persons names and where they are living ( names and locations separated by tap (it may not show perfectly below) ) . just like this example:
JhonWilliam NewYork
KhanFayre Bangalore
LuiseRamsy NewYork
so i wanted to write a PHP function to read that file line by line and only print(output to another file) people who live in NewYork
i wrote this function to read the file line by line
function process_file($input)
{
$handle = #fopen("input.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo process_line(trim($buffer));
}
fclose($handle);
}
}
process_file("input.txt");
?>
my question is .. How can i write a function to filter the text in the input file? because i only want to print the people who live in NewYork
You can use strpos to find out whether if an string contains another string.
function process_line($str, $findme) {
if (strpos($str, $findme) !== false) {
return $str;
}
}
And
echo process_line(trim($buffer), 'NewYork');
$handle = #fopen("input.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$n = explode('\t',$buffer) // $n[0] = name, $n[1] = city
if ( $n[1] == "NewYork" ) {
echo $n[0] . "\n";
}
}
fclose($handle);
}

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