I have a txt file set up something like this:
fence
canter1
edger
I currently read from it using file() and what I am trying to do is use the data (eg. canter1) to output a pre-defined div that is stored in an array. So I get canter1 which is in $data and want to output
echo $div[$data];
I get an undefined index error though I know that they exist because I can go
echo $div['fence'];
and everything is fine. The only time it doesn't give me an error is for the last line in the text file. In this example 'edger' would be valid. Any thoughts?
Instead of just using file() use file($file, FILE_IGNORE_NEW_LINES).
Related
I'm new to php. The thing I'm trying to accomplish here is to see what is the number of the last line in my html document (1,2,3...) and put that value into a variable and write
Hello world to the html document but 2 or 3 lines above the last line. So right above the body close tag. I have been trying for days now and nothing seems to work.
sampletext.html
<html>
<head>
<title>WEB</title>
</head>
<body>
<p>WEB test 335</p>
</body>
</html>
This one doesn't output:
<?php
$file = new SplFileObject('sampletext.html');
$file->seek(3); // Seek to line no. 4
echo $file->current(); // Print contents of that line
?>
I also tried this one ,but it only writes at the end of the file:
<?php
$file = fopen("sampletext.html","a"); //I also tried w,a+,r,r+
$txt = "Hello world";
fseek(fp, 0, SEEK_END);
fwrite($file, $txt);
fclose($file);
?>
There are a more but I can't seem to find them in my testing folder.
I have read somewhere that it's possible to move the last few lines into temp memory and fwrite something and then move the lines from temp memory back into the file.
As I mentioned before I am very new to php and don't fully understand it yet!
Provided that this is something you would actually want to do, I would create an array of all lines in the read file, determine it's length, change the content of the third last array item and output everything in a loop.
That said, the way you seem to want to use PHP seems very non-standard. If you would read a bit about it, I bet you would come up with much more elegant solutions than this. A good starting point would be w3schools
The only thing that I found that works is to delete the last few lines of the code, write what you wanted and then write the code you deleted or use a php function that reads through the file and looks for a specific code (variable or something else but it shouldn't repeat in that context) and change it with another bit of code.
Try this to get last line: PHP - Returning the last line in a file?
And for write to last line try: Delete last line and check data in text file
I am reading data from a htm page into a Wordpress post with <?php include("liveresults/140403F001.htm"); ?> it works perfectly. I however need to skip the first 12 lines of the html file and only start reading from line 13. Any ideas?
If the file only contains HTML and you don't want to execute it as PHP, then you should not be using include anyway. readfile() is much better suited for that.
However, since you want to ignore the first 12 lines, you should use an SplFileObject which allows you to seek by line:
$file = new SplFileObject("liveresults/140403F001.htm");
$file->seek(12);
$file->fpassthru();
Note that if your file comes from some sort of external input, you should escape it for HTML, to guard against XSS.
I m using the php function file_get_contents to parse a php file. But it seems that as soon as it is reading the php tags the file_get_contents is malfunctioning.
I checked the function with a normal text file, its functioning perfectly. But even if it finds php tags in a text file, the file is being half read. How can i find a way to get the full contents.
Is the file local? Or are you trying to get a remote file? How did you check that the content is not read? Echoing it to a browser might trick you because of the < char in <?php
Use htmlspecialchars or <pre> to view the whole text. Or just look at the source of the page.
I have a code in my CMS that prints content:<?php print $content ?>
I would like to output the actual php and html code behind $content, ideally in the browser. What I mean here is not the result in the browser, but the actual code behind it.Is it possible at all?
EDIT: Just to explain further: I need to print the source code of $content. Basically this variable produce some html and php content. I would like to see the code it produces, change it and replace $content with my custom code. Ideally the source code should be printed in the browser, is there anny php function that does it?
First off install the Devel Module, it has a wonderful function called dpm() which will print the contents of any variable to the Drupal messages area.
Then you need to go into your theme's template.php file and implement hook_preprocess_page():
function mytheme_preprocess_page(&$vars) {
dpm($vars['content']);
}
That will print out the $content array before it's rendered into a string. In the same preprocess function you can also change $vars['content'] as you see fit, and the changes will be reflected in $content in page.tpl.php.
Hope that helps
What do you mean by 'the code'? I think what you want to do is not possible, unless you make some kind of quine it's not possible to output the actual php code of a php file when you run it.
If $content is something like:
$content = 3 + 4 + 5;
echo $content; will output 12 yes? But I'm taking it you want to output 3 + 4 + 5 or something along those lines. The thing is, PHP (although it doesn't feel like it) is compiled. In this trivial example, 3 + 4 + 5 is stored exactly nowhere in your compiled program, it is stored as 12 (since it's static). More complex lines of code will be stored as pointers, values etc., all in nicely obfuscated machine code. Getting back to the 3 + 4 + 5 requires reading the input file and outputting the relevant line, which is difficult (think about what happens if you add or remove some lines, or how your running program knows where in the source file it is, or even if it's in the right source file).
tl;dr: this is not possible.
Well, if you just want to see html source for $content, you should simply use htmlspecialchars :
echo htmlspecialchars($content);
http://php.net/htmlspecialchars
or http://php.net/htmlentities
I am trying to make a news feed type thing in php.
I have a text file - news.txt and a php file index.php.
I have done the surrounding code and opening/closing the text file. Now I am stuck how to insert the new news item $newsnew to the top of the news.txt file and how to delete the old bottom news file in the news.txt file.
Is there any way to do this without deleting the whole file and writing it all again?
EDIT: Each news item is just a small string, say 500 characters, a single line.
Use a database.
If you really must use text files, use a different file for every news-item and name them sequentially like:
news001.txt
news002.txt
etc.
Then you can just add and delete files, read the directory and display what´s there.
Use the file() function to import the items in news.txt as an array, and use array_unshift() to add the new first item, and array_pop() to remove the last item. Join the array back into a single string and write it to news.txt:
$items = file('news.txt');
array_unshift($items, 'New item 1');
array_pop($items);
$newstext = implode(PHP_EOL, $items);
// write $newstext to the external file
If this is a XML file you could read it, parse it and delete the last child in the DOM. But if you have all your data in a DB it could be much easier to rewrite the file every time.
EDIT: after your edit: yes, you can do it like this:
write your new line to a new file
read the old file line by line and write it to the new one
skip the last line (detected by counting or EOF)
delete the old file and rename the new
No, there is not. But you might consider storing the messages in revers order. That way you only need to append to news.txt when new news arrive.
You are not going to be able to prepend to the beginning of the file without writing the whole thing out again. You could append to the end of it with the "a" mode flag to fopen(), but still to delete the oldest item you'll need to write out the entire file again.
Really, a database is solution here instead of a single text file.
There are many ways you can do it using the flat text file, but I'm not really sure it it's worth it. You can use some lightweight structured file or embedded database. For example SQLite, which would store it in normal file, no additional setup needed.