Hi this is my problem i want to read from a file till i reach a specific character and then write a string in a new line before that specific character with php i know how to read by fopen i also know how to read line by line i dont know the last part(inserting my string in the line before that)
Look at This example please:
MYfile Contains:
Hello
How are You
blab la
...
#$?!
other parts of my file...
so know i want it to when it reached $?! put my string in the line before that assume that my string is I did it!
Now MYfile Contains:
Hello
How are You
blab la
...
#I did it!
#$?!
other parts of my file...
How should i do this?!?
What i’ve done so far:
$handle = @fopen("Test2.txt", "r");
if ($handle)
{
while (($buffer = fgets($handle, 4096)) !== false)
{
if($buffer == "#?") echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
You simply need to search for the
$?!when reading the text.As you are reading line by line, check it in each line.
Personally, I would read the whole file at once (assuming it is not too big) and replace the string with the vlaue required.
Not tested the above code – might require tweaking.