PHP preg_replace alway's overwrite my read file - php

I have a TEXT file bla.txt
REGION_NAME = 'bla_2'
hello
hi
goodbye
my script##
<?php
$file = 'bla.txt';
$file_contents = file_get_contents($file);
$fh = fopen($file, "w");
$file_contents = preg_replace("/(REGION_NAME =) ('')/", "$1 'us-east-A'", "REGION_NAME = ''");
fwrite($fh, $file_contents);
fclose($fh);
?>
When I run it, It adds the correct text but then overwrites the bla.txt file
so and I loose the other words.
NOTE:### If I use str_replace works fine but once bla_2 changes then we can't use the script again...
Any Idea...?
$file_contents = str_replace('bla_2','bar',$file_contents);

If you read the code you are overwriting the initial value of $file_contents, not replacing against the data in $file_contents. What you need to do is: $file_contents = preg_replace("/(REGION_NAME =) ('')/", "$1 'us-east-A'", $file_contents);. And as a disclaimer I didn't actually run your code but you weren't passing in the correct subject for the function from what I see.
And I think what you want for regex is something along the lines of preg_replace("REGION_NAME = '(.*)'", 'us-east-A', $file_contents)

Related

How to str_replace a txt file with newlines using PHP

I have a text file with the following 2 lines
woof
woof99
I then have this code where it's supposed to take the username, example woof and take it out and replace it with nothing.
$file = fopen("../UsersProfile/".$MyUsername."/otherfiles/Following.txt","a") or die("Unable to open file");
$content = file_get_contents("../UsersProfile/".$MyUsername."/otherfiles/Following.txt");
$newcontent = str_replace($Username."\n", '', "$content");
file_put_contents("../UsersProfile/".$MyUsername."/otherfiles/Following.txt", "$newcontent");
fclose($file);
the text file will then look like this with one line now.
woof99
the issue that I am having is that it isn't working for some reason. I believe it worked before but for some reason now it isn't. Is there an easier way to do this?
Thanks!
Edit: I found out what I needed to do I need to do \r\n
$newcontent = str_replace($Username."\n", '', $content);
Just turn this "$content" to this $content :D

file_get_contents() skipping text between <> tag

I am trying to read a .tsv file using PHP. I am using the simplest method of file_get_contents() but it is skipping any text between <> tags.
Following is the format of my .tsv file
<id_svyx35_88c_avbfa5> <Kuldeep_Raval> rdf:type <wikicat_Delhi_Daredevils_cricketers>
Following is the code I am using
$filename = "access_s.tsv";
$content = file_get_contents($filename);
//Split file into lines
$lines = explode("\n", $content);
echo $content;
On reading it, the output is just
rdf:type
Please help in what can be the solution to read the line as it is?
Try to apply htmlspecialchars() to $content:
$filename = "access_s.tsv";
$content = htmlspecialchars(file_get_contents($filename));
//Split file into lines
$lines = explode("\n", $content);
echo $content;
Reference on php.net
The tags have always been there, the browser just does not show them. Just like with any valid HTML tag, you can see them when viewing the source code of the website.

Add comma to the end of every line in a text file PHP

I started learning PHP this week and need to add a comma to the end of every line in a text file. I don't really know where to start. What would I do after I load the file? I can't find any tutorial saying how to write to the end of every line only the beginning of the file or end?? Is it possible to specify the end of a every line?
What I have so far:
fopen("file.txt",a)
????
I'm using "a" because I want to preserve the data.
This should work for you:
(Here I just get all file lines with file(). Then I go through each line with array_map() and concatenate a comma at the end. After this I save the file with the new content with file_put_contents())
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES);
$lines = array_map(function($v){return $v . "," . PHP_EOL;}, $lines);
file_put_contents("test.txt", $lines);
?>
Example file:
a
a
a
after the script:
a,
a,
a,
EDIT:
If you don't want to add a comma to an empty line just add a condition to check if the line is empty or not like this:
<?php
$lines = file("test.txt", FILE_IGNORE_NEW_LINES);
$lines = array_map(function($v){return $v . (empty($v)?"":",") . PHP_EOL;}, $lines);
file_put_contents("test.txt", $lines);
?>
Do you want to replace all the EOL with some specific character ?
$EOLChar=",";
$myFile = fopen("file.txt", "r") ;
$newFile="";
while(!feof($myFile)) {
$newFile.= str_replace(PHP_EOL, $EOLChar, fgets($myFile));
}
fclose($myFile);
file_put_contents("file.txt", $newFile);

Php putting amount of characters after string

I'm a front end developer but I'm learning php so I can do basic back end stuff. My code seems to be putting the amount of characters after the echoed string.
<?php
$openFile = fopen("file.txt","w");
$name = "Chris";
fwrite($openFile,$name);
fclose($openFile);
$openFile = fopen("file.txt","r");
echo readfile("file.txt");
fclose($openFile);
?>
This is what I get for posting here. -2.
Live preview: http://icodewebsitesandineedatestingserver.netai.net/
I guess you're trying to read the contents of the file?
PHP doc on readfile:
Reads a file and writes it to the output buffer.
5 is the number of bytes read.
You can use file_get_contents:
<?php
$openFile = fopen("file.txt","w");
$name = "Chris";
fwrite($openFile,$name);
fclose($openFile);
echo file_get_contents("file.txt");
?>

In PHP when using fgets() to read from file how to exclude "brake row"

I'm writing simple function witch will read data from myfile.txt with fgets().
Content of file is something like:
1
2
3
4
Function to get first value (1):
$f = fopen ("myfile.txt", "r");
$mystring = fgets ($f);
Now, when I use $mystring to write in file like:
$f1 = fopen ("myfile2.txt", "w");
fwrite($f1, 'text' . $mystrin . 'more text');
text 'more text' goes to new row.
I want to have it in in same row with other text.
When reading a file using fgets() it will include the newline at the end. You only need to remove it.
$mystring = trim ($mystring);
This will remove any leading and trailing whitespaces.
$mystring = rtrim ($mystring, PHP_EOL);
Will remove any trailing newlines.

Categories