I'm working on a project that takes text files from a folder and searches the text of each file for specific keywords that correspond to image files that I'm trying to insert in the text as an html image tag using php.
I got it almost working; the correct images get inserted at the desired points, and text files that do not contain a keyword remain untouched, but the text files that have images inserted are doubled: one text file with added image and one original text file.
I have tried different approaches but either it doesn't work at all, or the text files with inserted images are duplicated.
Where lies the fault? Am I going at it the wrong way?
Here is my code:
$path = // array of .txt-filenames with plain tekst
$imagepath = // array of .jpg-filenames
foreach ($path as $file) {
$haystack = file_get_contents("$file");
foreach ($imagepath as $imagefile) {
$needle = $imagefile;
$position = strpos($haystack,$needle);
if ($position !== false) {
$output = substr_replace($haystack, "<img src='$imagefile'>", $position, 0);
} else {
$output = $haystack;
}
echo $output;
}
}
If you have two images in $imagepath
and the second one is not found by strpos
then I believe $output = $haystack;
will revert previous changes.
Here's a code snippet with more meaningful variables.
<?php
$textFiles = [];
$imageFileNames = [];
foreach ($textFiles as $file) {
$fileContent = file_get_contents($file);
foreach ($imageFileNames as $imageFileName) {
$position = strpos($fileContent, $imageFileName);
if ($position !== false) {
$fileContent = substr_replace($fileContent, "<img src='$imageFileName' />", $position, 0);
}
}
file_put_contents($file, $fileContent);
echo $fileContent; // if you want to debug
}
We could argue that we're rewriting $file
even if $fileContent
has not changed is useless but that's another story!