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.
Related
I have spent the last several hours pulling my hair out trying to figure out the solution to this problem. I am sending an AJAX request which, up until some minor changes, worked perfectly, returning a lovely usable character to the Javascript. Now, however, a \r\n is being returned, and I have spent far too long tracking it down. My final method for finding where it was being included was literally echo-ing "OMG" in various places around my scripts until it showed up on Line 2 of the HTML instead of Line 1. Here is the offending script:
// Import Global Game Variables
include('../engine/engine_core_functions.php');
// Convert our gamestate(gameID)
//$curGamestate = getCurrentGamestate($gameID);
// Make sure it's a valid turn
if(isMyTurn()) {
// Draw a card from the card drawing mechanism
$cardValue = drawCard();
$cardValue = str_replace("\r", 'R', $cardValue);
echo $cardValue;
}
else echo 'Error 3';
The line skip occurs immediately after the include file at the top. Before the include, no line break, after the include, line break. So I go to the include file. Placing my
echo 'OMG!';
at the VERY END of the included file does NOT produce a line break. Which led me to believe that including a file may (why!?) generate a line break (it's 5 AM...). However, there are multiple included files at the top of the offending included file. None of them generate breaks. The entire "engine_core_functions.php" generates no line breaks at all.
However, a break shows up when it is included in the above-shown script. Needless to say, I'm baffled and extremely annoyed. I could simply remove the offending characters (via PHP or Javascript) but it annoys me I can't seem to fix the root of the problem. Please help, thank you.
You could have some kind of invisible BOM mark at the beginning of your file or something else.
Always let <? or <?php be the first string of your PHP files and make it a practice NOT to end the entire PHP file with ?> if it's going to be included by another file.
I am using a html minifier, which can be found here: HTML minify
The strange thing to me is that every tag is placed on a new line. Is this common behavior or am I doing something wrong. The output looks something like this:
Anyone know how I can fix this so that is just creates one line of code, or is has this was of minifying some advantages.
Checked the code?
// use newlines before 1st attribute in open tags (to limit line lengths)
$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
Long lines can be a bad bad thing - browsers might fill buffers or just drop stuff at the end of the line. So it looks like that Minify script has it hard coded in, with no options to change. So if you really want it all on one line, just customise your version to not do that replacement. Open Source win.
Let's say I have a file "English.txt" containing these lines :
$_LANG["accountinfo"] = "Account Information";
$_LANG["accountstats"] = "Account Statistics";
Note : the file extension is .txt and there is nothing I can do to change that. There is no opening PHP tag (<?php) or anything, just those lines, period.
I need to extract and actually get the $_LANG array declared from these lines. How do I do that? Simply includeing the file echoes every line, so I do
ob_start();
include '/path/to/English.txt';
$str = ob_get_clean();
Now, if I call eval on that string, I get an syntax error, unexpected $end. Any ideas?
Thanks.
eval(file_get_contents('English.txt'));
however, be sure NOBODY can change English.txt, it could be dangerous!
First of all, note that you should use file_get_contents instead of include with output buffering. Since it contains no <?php tag, there is no need to run it through the script processor.
The following works perfectly in my tests:
<?php
$contents = file_get_contents("English.txt");
eval($contents);
var_dump($_LANG);
As one of the comments said, if you do the above and still get an error, then your file does NOT contain exactly/only those lines. Make sure the file is actually syntax compliant.
As has been mentioned, you should really use eval only as a last resort, and only if the file is as safe to execute as any code you write. In other words, it must not be editable by the outside world.
I want to create a PHP script that grabs the content of a website. So let's say it grabs all the source code for that website and I say which lines of code I need.
Is there a function in PHP that allows you too do this or is it impossible?
Disclaimer: I'm not going to use this for any illegal purposes at all and not asking you too write any code, just tell me if its possible and if you can how I'd go about doing it. Also I'm just asking in general, not for any specific reason. Thanks! :)
file('http://the.url.com') returns an array of lines from a url.
so for the 24th line do this:
$lines = file('http://www.whatever.com');
echo $lines[23];
This sounds like a horrible idea, but here we go:
Use file_get_contents() to get the file. You cannot get the source if the web server first processes it, so you may need to use an extension like .txt. Unless you password protect the file, obviously anybody can get it.
Use explode() with the \n delimiter to split the source code into lines.
Use array_slice() to get the lines you need.
eval() the code.
Note: if you just want the HTML output, then ignore the bit about the source in step 1 and obviously you can skip the whole eval() thing.
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.