Php putting amount of characters after string - php

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");
?>

Related

PHP preg_replace alway's overwrite my read file

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)

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.

Read text file and print in the same format

i want to read a text file on a php page and print (echo) the content in the same form (with the paragraphs). So i tried two implementations
Code 1
$textfile = "teste.txt"; // Declares the name and location of the .txt file
$content="";
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w ');
if (is_readable($textfile)) {
$content .= fread($fh, 8192);
echo $content;
} else {
echo 'The file is not readable.';
}
fclose($fh);
?>
Using this code nothing apper on the screen. and i had another problem. If i used filesize ($textfile) on the fread i had this error Length parameter must be greater than 0.So i guess this is the problem. but the text file has content
Code 2
<?php
$homepage = file_get_contents('teste.txt');
echo $homepage;
?>
This code works. but the format is not what i want.
the echo prints this
25 ºC 26 ºC 27 ºC 26 ºC 26 ºC
I want that appear one value on each line like i have on the text file.
What can i do to acomplish that
Thanks for the help
note sure if this is answerd, but try this
<?php
$file = "LOCATION/NAME.txt";
$text = file_get_contents($file);
$text = nl2br($text);
echo $text;
?>
This should be as simple as wrapping the output inside the PRE tag:
<?php
$homepage = file_get_contents('teste.txt');
echo '<PRE>' . $homepage . '</PRE>';
?>
This will work better than nl2br() if you also need to keep the exact number of spaces due to needing a fixed-width font. HTML normally turns two or three spaces in a row into just one. But inside a PRE tag if you have 3 spaces, all 3 will be displayed.

How to write file as it is?

I have a string of code like this:
<?php
echo "Hello World";
?>
Now, I want to write that into a php file just the way it is. What I mean is that I want to write this code with new line characters. So the code gets stored in the above fashion. So, how can I do that?
I had tried to use file append and fwrite, but they write the code in one consecutive line and what I want is to have it divided into 3 lines.
On the first line - <?php
On the second line - echo "Hello world";
On the third line - ?>
But I would like to do it by using only one line of code, that would be something like the below one.
$file = 'people.php';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
This should work fine using an heredoc:
file_put_contents('people.php', <<<HEREDOC
<?php
echo "Hello World";
?>
HEREDOC
);
<?php
$File = "new.txt"; //your text file
$handle = fopen($File, 'w');
fwrite($handle, '<?php '."\n".' echo "Hello World "; '."\n".'?>');
?>
Your string should be like this
$str = "<?php
echo \"Hello World\";
?>";
then write it to any fileName.php, php while parsing ignores new lines, only semicolons matters.
Edit 1
Since you want to write your string as code, after following above step you will have all your code in a single line more like a compressed code, which will be not human readable, for making it human friendly, you will have to add formatting, a minimal approach for formatting will be to at least add new line chars and tabs for indentation.
for this you will have to do two things, identify chars (a) where you need to add new line feed and those chars (b) where indentation is required. Now before writing to file do some preprocessing add new line chars for char type (a) and at the same time maintain a stack for adding proper number of tabs for indentation.
Try using the following between each line of code to put it on a new line in the php file:
. PHP_EOL .
You can:
<?php
$yourContent = <<<PHP
<?php
echo "Hello world";
echo "This in an example";
$foo = 2;
$bar = 4;
echo "Foo + Bar = ".($foo+$bar);
?>
PHP;
file_put_contents("yourFile.php", $yourContent);
?>
Try following :
<?php
$file = "helloworld.php";
$content = file_get_contents($file);
print_r(htmlspecialchars($content));
?>
Helloworld.php :
<?php
echo "Hello World";
?>
It'll work fine.
It sounds like you need to add a line break to the end of each line. You can use a regular expression search/replace as follows (where $newData already contains the string you want to append to the file):
<?php
$newData = preg_replace('/$/',"\n", $newData);
file_put_contents($file, $newData, FILE_APPEND | LOCK_EX);
?>
This finds the end of each line (indicated by $ in regex) and adds the new line (\n) at that position.
this code is working on my pc. you will also like it.
<?php
$file = 'people.php';
// The new person to add to the file
$person = "<?php
echo 'Hello World';
?>\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
echo $person;
?>

I am using fgets() to read in a line in PHP. But i am getting a wierd result?

$size = intval(trim(fgets($fp,4)));
$triangle = range(1,$size);
for($j=0;$j<$size;$j=$j+1)
$triangle[$j] = split(" ",trim(fgets($fp,400)));
This code reads in the number of lines to read, then reads them one by one. Issue is, when first input line ends in space, it reads that space as a new line.
you can read full file content by file_get_contents.
<?php
$content= file_get_contents('myfile.txt');
echo $content;
?>

Categories