I am using the loadhtml function (http://php.net/manual/en/domdocument.loadhtml.phpt) to load up an external .html file. When I load it, it "tidy's" up my code, which, I don't want. I do NOT want a full HTML document, I only want html snippets in my .html, and I don't want the loadhtml file to try to make it valid html, because I don't want it to.
Is there a better function to load up a .html file so that it does not tidy up the code?!
If you want to just put the HTML into a string, you can just use:
$file1 = file_get_contents("file.html");
LoadHTML puts your html file data into a DOM structure.
If it is not really HTML (snippets aren't) then you can't really make a DOM structure -- but that is what you asked for.
Now, the first question comment asked what you wanted to do once this data was loaded, so either you have a good answer for that which will point us in a new direction, or the answer is simply NO.
To store:
$contents = file_get_contents('example.html');
To output:
readfile('example.html');
Related
One of my file is read by php and loaded on the Web UI. But if the file contains HTML tags, it interprets and disturbs the whole UI.
How to ignore the HTML tags while reading the file contents.
I am using following code to read file contents:
readfile($name);
I think what you are looking for is htmlspecialchars function.
e.g. echo htmlspecialchars($stringFile);
If you want to strip all html tags, check strip_tags function.
I have an HTML file that has a rather long navigation menu inside of it. I want to take that menu out of the HTML and place it into an external PHP page and then call it with
<?php include 'navigation.php'; ?> in the HTML file.
I have tried just adding this into the HTML file but it doesn't display anything as well as no errors on the page.
What do I need to do (if it's even possible) to keep the files HTML and use the php require function?
Add this in in your httpd.conf and then you can process PHP code on HTML pages
AddHandler application/x-httpd-php .php .html
Q: Did you give the page a .php suffix? That should be all you need to do.
Remember the way PHP works - you basically "embed" your PHP code in an HTML page, and the server executes the PHP before it serves (the rest of) the HTML.
But in order for PHP to "see" your code, you need to make sure your "HTML page" has a .php suffix.
As a crude workaround, you can add ".html" to the list of file suffixes that PHP will parse.
But this could cause other things to break.
If you want to embed PHP code in your "index.html", the best, cleanest approach is to simply rename it "index.php".
IMHO...
I'm using PHP Simple HTML DOM Parser to get the contents of the first <h1> tag on different webpages. The script works great most of the time, but on some webpages my script just kind of 'hangs up'. The script stops, without completing the code that comes after what I have listed below. I looked at the source for the pages that don't work, but there is nothing particularly different about the <h1> or its contents. Is there a way I can get this to work for all possible URL's, and if not, how can I fix my script so it won't hang up for the URL's that don't work?
include_once( 'simple_html_dom.php');
$html = file_get_html($webpage);
$element = $html->find('h1', 0);
$element = strip_tags($element);
I never got a response, but it appears the answer is to use cURL to get the contents of the url, then get the tag info with PHP Simple HTML DOM Parser.
I am using PHP in combination with Smarty Templates to generate pages serverside. Currently, I am loading a page as follows:
$smarty->assign('app', file_get_contents("some_content.php"));
Where some content contains HTML with PHP tags and code inside those tags.
I would like the PHP content inside this file within the current scope (That of the script reading the file), so that a particular function I've defined is available. How would I go about doing so? All the information I can find is regarding the eval(...) function, which doesn't seem to be able to cope with the HTML/PHP mixture: would I need to perform a find/eval/replace operation to achieve the desired result, or is there a more elegant way of doing this?
From my opinion, this short snippet of the code you posted shows that something is generally wrong there :)
But nevertheless you can achieve whatever you are trying to achieve by doing the following:
ob_start();
include("some_content.php");
$result = ob_get_clean();
$smarty->assign('app', $result);
Ich, I'm such a dummkopf. There is an answer right on the PHP manual for eval, right under my nose. Here is the answer I neglected to notice.
You can use {literal}...{/literal} smarty tags to display any content in smarty templates as is. It used to transfer java scripts and other specific content.
I'm having an html file, index.php I want to take the content within a <div> with the class main of that file and replace it with another text. How can i achieve that?
Sample content in html:
<div class="main">
Replace this text with some code!
</div>
I want get the content within this div using php and replace it with another content. But I have no idea on how to do this.
Update:
I'm aware of client side trick with javascript. I want to do this server side. And the file will be html and not php. so I think i have to open the html in php and do this, though i don't precisely how.
Can this be done with xpath or html dom parser or something? A google search gave me these terms but i have no clue what they actually are.
You can use PHP's DOM classes/functions to do this.
Start by creating/loading your document:
$d = new DOMDocument();
$d->loadHTML($yourWellFormedHTMLString);
Then you'll want to locate the document node that you want to alter. You can do this using XPath:
$xpathsearch = new DOMXPath($d);
$nodes = $xpathsearch->query('//div[contains(#class,'main')]');
Then you'll want to iterate over matching nodes, and create new nodes inside:
foreach($nodes as $node) {
$newnode = $d->createDocumentFragment();
$newnode->appendXML($yourCodeYouWantToFillIn);
$node->appendChild($newnode);
}
If you don't mind messing around with a library at an early stage of development, take a look at CAST (content-addressed style templating). It's pretty much designed to do what you're describing, and if nothing else, you could peer inside the source to see examples.
(NOTE: I'm sure the astute will note that //div[contains(#class,'main')] isn't exactly the equivalent of the CSS selector div.main ... since the class attribute can contain more than one class. Doing this precisely is unwieldy enough I think it's better to start with the simplified expression when you're introducing people to it, even if it might best for those who go this route to eventually get to know xpath well enough to handle this right. Or, just use ids more instead of classes. :)
If it just needs to include a static fragment
<div class="main">
<?php readfile ('path/to/some/file'); ?>
</div>
If it needs to include the output of another PHP script
<div class="main">
<?php include ('path/to/some/file') ?>
</div>
You read the file with:
$fileContents=file_get_contents($file_path);
http://php.net/manual/en/function.file-get-contents.php
Then you search and replace the div content:
$newHtmlContent=preg_replace("/<div class=\"main\">(.*)</div>/i",'<div class="main">Some text here</div>',$fileContents);
http://php.net/manual/en/function.preg-replace.php
My regular expression is a little rusty, but you can scoop it up in here:
http://www.regular-expressions.info/tutorial.html
Then save the new content:
file_put_contents($file_path,$newHtmlContent);
http://www.php.net/manual/en/function.file-put-contents.php
Or you could parse the file using this:
http://simplehtmldom.sourceforge.net/
But it must be well formed.
I would recommend this version as the above will fail if the contet of the main div is another div...