We've 700 static HTML files in folder "music". We want to put analytic code in the end of the HTML files.. like before
</body> </html>
tags.
Please anybody let me know, how its possible with PHP code?
Too easy. Find all files, read content, modify content, save content.
<?php
// open directory
if($handle = opendir(__DIR__)) {
// search for
$search = '</body>';
// replace with (</body> gets appended later in the script)
$replace = <<< EOF
<!-- your analytics code here -->
EOF;
// loop through entries
while(false !== ($entry = readdir($handle))) {
if(is_dir($entry)) continue; // ignore entry if it's an directory
$content = file_get_contents($entry); // open file
$content = str_replace($search, $replace . '</body>', $content); // modify contents
file_put_contents($entry, $content); // save file
}
}
echo 'done';
?>
Related
I am trying to replace some hyperlinks in a csv file, like this one:
[https://assets.suredone.com/683987/media-pics/6164307j-gabriel-61643-proguard-steel-shock-absorber-for-select-chevrolet-gmc-models.jpg. Here is my code:][1]. Here is my code:
<?php
$in_file = 'gabriel-images-urls.csv';
$out_file = 'results.csv';
$fd = fopen($in_file, "r");
$new_array= array();
$toBoot= array();
while ($data = fgetcsv($fd)) {
echo '<pre>';
if (strpos($data[2],'media-pics') !== false) {
$data[2]=str_replace('media-pics','media-photos',$data[2]);
fputcsv($fd, $data);
// echo $output;
}
}
?>
The new link for example must look like this:[1]https://assets.suredone.com/683987/media-photos/6164307j-gabriel-61643-proguard-steel-shock-absorber-for-select-chevrolet-gmc-models.jpg. The goal is he "media-pics" substring to be replaced with "media-photos". At this point nothing happens in the file. I think this is because the file is open only for reading but I am not sure.
Can you not simply do a string replacement on the whole file rather than attempting to load and process each line of the file using fgetcsv?
<?php
$srcfile='gabriel-images-urls.csv';
$outfile='results.csv';
$csvdata=file_get_contents( $srcfile );
$moddata=str_replace('media-pics','media-photos',$csvdata);
file_put_contents( $outfile, $moddata );
?>
Here is the script normally. It saves data to a file.
<?php
if (isset($_GET['field'])) {
$file = 'data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= "\n".$_GET['field'];
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
But I want it to add (added by a user) at the end of what is sent. I have tried this:
<?php
if (isset($_GET['field'])) {
$file = 'data.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new line to the file
$current .= "\n".$_GET['field'] + "(added by a user)";
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
But it doesn't work. How do I do this?
A (simple) question.
I have a TXT file search script in PHP.
$search = $_GET["search"];
$logfile = $_GET['logfile'];
// Read from file
$file = fopen($logfile, "r");
?> <head> <title>Searching: <?php echo $search ?></title> </head> <?php
while( ($line = fgets($file) )!= false)
{
if(stristr($line,$search)) // case insensitive
echo "<font face='Arial'> $line </font><hr>";
}
fclose($file);
Now what I want to do is delete all the text it finds in the TXT file.
I tried doing a str_replace but it didn't work.
Thanks for helping!
I think this will do the magic:
$file->ftruncate($file->ftell());
You need to gather the lines which doesn't contain the search term and you need to save the text in an array.
When you're done with the listing, you need to open the file in write mode (which will empty the file) and then write the text you gathered in the file.
Here's the code:
<?php
$search = isset($_GET["search"]) ? $_GET["search"] : '';
$logfile = isset($_GET['logfile']) ? $_GET['logfile'] : '';
$text_without_term_arr = array();
if(!empty($logfile) && !empty($search)){
// Read from file
$file = fopen($logfile, "r");
echo ' <head>
<title>Searching: ' . $search . '</title>
</head>';
while(($line = fgets($file))!== false){
if(stristr($line, $search)){
// Case insensitive search
echo '<font face="Arial">' . $line . '</font><hr/>';
} else {
// Search term not found in these lines
array_push($text_without_term_arr, $line);
}
}
fclose($file);
// Empty the file and write the text again without the search term
if(!empty($text_without_term_arr)){
$new_file = fopen($logfile, "w");
$content = implode("\n", $text_without_term_arr);
fwrite($new_file, $content);
fclose($new_file);
}
}
?>
You need another file handle to write the result out to:
$tempname=tmpname('/tmp','result');
$outfile=fopen($tempname,'w');
Next, use str_ireplace to remove the found text in each line:
$newline=str_ireplace($search, '', $line);
Then write the new line out to the out file:
fputs($outfile,$newline); // May need to send PHP_EOL too
Close the file off:
fclose($outfile);
And then rename the new file to the old filename:
rename($tempname,$logfile);
I'm writing some code that can read in from a .txt file a display it on a webpage.
I had problems in my initial code, in that it would read in any text and it would erase whatever was in the document.
My original code:
function readIn(){
$input = fopen("input.txt", "r"); //Open the file, save opened file in input
$line = fgets($input);
fclose($input);
return $line
}
It only started working once I put in a While loop to go through EVERY LINE
function readIn(){
$input = fopen("input.txt", "r"); //Open the file, save opened file in input
$fullText = ""; //Variable full text
while(!feof($input)){
$line = fgets($input);
$fullText = $fullText . $line;
}
fclose($input);
return $fullText;
}
echo readIn();
Use "file_get_contents" to read an entire file into a variable, and then output in whatever fashion you choose.
This code enables to create a new .txt file, if the file doesn't exist yet, it will create the file.
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
And this code here identifies each line of string into a token.
<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, " \n\t");
while ($tok !== false) {
echo "Word=$tok<br />";
$tok = strtok(" \n\t");
}
?>
The people.txt looks like this
John Smith
John Meyer
John Chase
John Smith //i want to transfer this set of string into a new file
What am i missing here?
$searchTerm = 'John Smith';
$file = 'people.txt';
$fileFound = 'peopleFound.txt';
$current = file_get_contents($file);
if (strpos($searchTerm, $current) !== false) {
file_put_contents($fileFound, $searchTerm."\n", FILE_APPEND);
}
$fp=fopen('people.txt','a+');
fputs($fp,"John Smith\r\n");
fclose($fp);
open and append the file, don't read it all in and append to the back and write it all back out. when the file gets past a certain size that will be real inefficient. I'm not sure how it is you're trying to tokenize it. seems to me you could just do a $names=file('people.txt'); and have them in an array immediately...