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.
Related
I have a .prt (CAD Design File) that I need to extract some XML from using PHP. When I view this file directly in the browser, I can see the XML along with some unreadable areas. However, when I go to open it using PHP to get the XML I need from it, the file becomes mostly unreadable and the XML is no where to be found as the file looks like it was encrypted.
This is an example of what the .prt file looks like when opened directly in the browser: File in Browser
This is an example of what the file looks like when opened using PHP: Using PHP
This is how I am trying to open the file with PHP:
$handle = fopen("thePart.prt", "rb");
$contents = trim(stream_get_contents($handle));
fclose($handle);
//echo out contents to see what happens
echo $contents;
If I could get this file to open without doing what it is doing, I can get the XML out of it myself. How do I fix the issue that I am having? Thank you very much in advance.
Real Answer
Turns out that there was no problem at all with the code. The browser was just interpreting the XML tags as HTML and so the data was not displayed (PHP by default sets a content type of text/html). When viewing the source code, the XML was plain and visible. The XML can also be seen without viewing the source by setting the content type of the php file:
header('Content-Type: text/plain');
This way, the browser will just display the XML as it is, without attempting to parse it as HTML first.
Initial Answer
Just a guess here, but it might be that you're opening the file in binary mode (the "rb" in your first line of code. Try opening it as a plain text file (use "r" instead of "rb").
More likely, it's an encoding issue where PHP is trying to decode a UTF-8 file as ASCII, for instance. Since you are opening a binary file (CAD Design File is binary with a little XML, I'm assuming), PHP might be getting confused while trying to detect the encoding of the file. I would need a copy of the file to know for sure.
Try comparing the result of mb_detect_encoding:
mb_detect_encoding($contents)
and the actual encoding of the XML data within the .prt file. If they are different, that's how you know that PHP is using the wrong encoding. In that case, use mb_convert_encoding to convert from PHP's detected encoding to that of the XML data.
This question is in reference to:
Free (preferably) PHP RTF to HTML converter?
I'm trying to execute that last line of code in my php:
exec(rtf2htm file.rtf file.html)
I understand what parameters need to go within the parentheses, I just do not know how to write it. I've looked at multiple examples along with the php documentation and still I remain confused, so could someone show me how it is written? rtf2htm refers to a PHP file which converts RTF to HTML.
Ultimately what I am trying to do is convert the content of numerous RTF docs to HTML, maintaining the formatting, while not creating tags such as<head> or <body> which programs like Word or TextEdit generate when converting to HTML.
rtf2htm is not a php script, it is a program installed on the server. exec() is used to call external applications.
EDIT: After looking up this script, it seems that it is indeed a php script. But it has been coded to be usable from the command line only.
This should work:
<?php
exec('php /path/to/rtf2htm /path/to/source.rtf /path/to/output.html');
?>
I've written a script to process html files from URLs, however, due to a 30's script runtime restriction with my cheap host provider I've had to alter the script to store the html as txt files and run it from a local WAMP server.
I am trying to load each file up, extract what I need, then move onto the next file.
URL's as source file_get_html was doing the job perfectly (I could ->find the required elements)
Txt file as source file_get_html is returning a blank object.
Based on some advice in the below post I changed file_get_html for file_get_contents which created an array with a single large string containing the contents of the text file.
First, make sure that file_get_contents can get data. If it can, file_get_html will be able to load data to simplehtml Dom
If file_get_contents returns a string, which it does, how would I "load data to simplehtml Dom?"
File not getting read using file_get_html
I then tried to convert the string into an object str_get_html, however, this didn't work either.
include('simple_html_dom.php');
$html = file_get_html('file.txt');
var_dump($html);
Returns: object(simple_html_dom)[1] but with no other contents or arrays.
include('simple_html_dom.php');
$html = file_get_contents('file.txt');
var_dump($html);
Returns: string < ! DOCTYPE html PUBLIC.....
Questions:
Can anyone give me any advice? What's the best way to load up a text file containing html markup into an object so that I can utilise the find method on it's contents. I want to avoid loading the file into an array of strings and using regex to process contents.
Are there any considerations I need to make if using a local WAMP server?
(Answered by the OP in a question. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I managed to solve this myself. I am sure i'd already tried to extract html from string, doh!
include('simple_html_dom.php');
$html = file_get_contents('file.txt');
$html = str_get_html($html);
var_dump($html)
Returns object(simple_html_dom)[1] including all expected arrays etc
Instead of trying to create the html object directly from the source file using file_get_html I've extracted the file contents file_get_contents then converted str to html using str_get_html which allows me to use the simple html dom methods e.g. find on attributes within the object e.g.
$html->find('a');
Hello im struggling with a problem. I have an url that contains xml data...
when i'm using file_get_contents($url) or fopen($url,'r') it gives me only values:
Consider the xml:
<tag1 attrName="something">
<tag2>some Value</tag2>
<tag2>some Other Value</tag2>
...
...
</tag1>
what i get: some Value, some Other Value
But i need to get whole xml (with tags and attributes and its' values) and parse it with my own way because there's a restriction that i'm not allowed to use php 5.x practices.I mean i cant use any parser.. It shouldnt be so hard to get xml data as is.. should it??
what i get: some Value, some Other Value
Nope - my suspicion is that that is what you see in your browser, because it is swallowing all <tags>.
The XML source code will be there after a file_get_contents() operation.
You are using file_get_contents() which states
This function is similar to file(), except that file_get_contents()
returns the file in a string, starting at the specified offset up to
maxlen bytes. On failure, file_get_contents() will return FALSE.
Press Ctrl+u to see the source code in any of the major browsers(except IE where its F12 in IE9). I am sure that your code will be there. Your browser wont display the tags that's all.
The other longer(but better way) to display an XML file from your php file is to pass the content type as text/xml. Use the following way
<?php
header("Content-Type: text/xml");//SHOULD come before any output
// dynamically generate and output your xml here
?>
Very simply, i want to make a variable reads the html code as string ,, i mean dont execute it (run it) .
the problem with the code is : i have a html file , and i want to get the content of it , and make some preg_replace for it (run a function on the html code), the problem is i cant use preg_replace, or any another function because the html code is executed by php (php reads the html code)..
i wish you understand me, i want something like highlight_string, but it save the html code in the variable.
Thank you.
you're probably trying to include or require the HTML code.
which is incorrect since it is evaluated as part of the source.
instead, use a function such as file_get_contents() to read the file into a string.
Use file_get_contents() as #David Chan suggested and then pass the result through htmlentities()... it converts the characters to HTML entities (i.e., < to <).
$getTheContent = file_get_contents($filepath);
echo htmlentities($getTheContent);
It should return the code, not executed.