Load html page in php

Load an html page into/from a php page

See here Solution 2: Just follow follow the steps index.php dashboard.php Change your file name dashboard.html to dashboard.php and put $name variable in index.php file first then include dashboard.php file. For example: In dashboard.html in your index.php If you choose this way, there are already nice template engines like smarty. 3rd: Use DOM Objects in PHP and replace it.

Load an html page into/from a php page

You can just rename the file to index.php . It doesn’t require that any actual PHP code be in the file, the server’s PHP engine will still process the file (pretty quickly too, since there’s no server-side code to interpret) and show the HTML.

RewriteEngine on RewriteRule index\.html index.php [NC,R] 
DirectoryIndex index.php index.html 

In order for this to work you need to allow overrides for indexes in the apache config

Load html mail file inside php, I implemented a html email inside a html file. And i have a php file which uses the PHPMailer class to send emails. What i want to achieve is, i have some text …

Php: Loading php code inside HTML

Return the loadContent from function:

public function load($view,$data =[]) < extract($data); ob_start(); require_once PATH_APPLICATION . '/views/' . $view . '.php'; $content = ob_get_contents(); ob_end_clean(); $this->loadedContent[] = $content; return $this->loadedContent; > 

I just saw the new updated question now. Unless You are using a Framework which implicitly calls the Show Method, I’d say that You are not calling the Show method anywhere in your code. and from your code, the Show Method is responsible for echoing the output to the View Script.

Читайте также:  Array two values php

I’d suggest you perhaps call it in the load method after assigning values to the the array like so:

loadedContent[] = $content; //Push the contents of the Buffer to the internal $loadedContent Array > $this->Show(); // You should call the show method to echo the html. > public function Show() < foreach($this->loadedContent as $html) < echo $html; >> 

And then back in the View, you could try this:

I hope you find this helpful. Good luck and cheers.

How do I load an HTML file via PHP and insert data from, I have an HTML file we’ll call it dashboard.html. I have a PHP file called index.php that will load on the webserver by default. I have a mySQL …

How do I load an HTML file via PHP and insert data from mySQL?

There are many ways to it:

1st: Put your html code into your index.php and echo your data on the right place. Simpliest one.

2nd: Use Keys in your html and replace it with the data. For example:

$holeFile = file_get_contents('dashboard.html'); $holeFile = str_replace('%DATA%', $resultSet['column']); // . 

If you choose this way, there are already nice template engines like smarty.

3rd: Use DOM Objects in PHP and replace it. Mostly the same as 2nd, but in a nicer way. See here

Just follow follow the steps

$name= "xxxxx"; include "dashboard.php"; 

Change your file name dashboard.html to dashboard.php and put $name variable in index.php file first then include dashboard.php file. Hope it will solve your problem

PHP load html file when index.php loads, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams

Load HTML Source to String in PHP

//$url = 'https://www.youtube.com/'; $url = "https://www.youtube.com/watch?v=5XR7naZ_zZA"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $content = curl_exec($ch); curl_close($ch); if(preg_match("~watch-time-text~", $content))< echo "it's there"; >else < echo 'is another page'; >print document code: echo "
".htmlentities($content)."
"; // match whit html code in 'watch-time-text':   Solution 2: 

Yes, you're very close. Basically, just scrap the part where you're trying to load this into XML since the page code is HTML and not XML.

$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA'); if(preg_match("~watch-time-text~", $str)) < print "Match was found!"; >else

Unfortunately, I can't show you a demo since ideone.com and codepad.org aren't allowing me to use file_get_contents , but this works from my own server.

If you run into situations where file_get_contents is not allowed as I have, you can do as miglio said and use cURL to get the remote source. But the rest is the same:

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/watch?v=5XR7naZ_zZA'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $str = curl_exec($ch); curl_close($ch); if(preg_match("~watch-time-text~", $str)) < print "Match was found!"; >else

Scripting - How do I load content from a PHP file (and, All the links in the page access different "pages" using ?page=pageName and the index.php script loads HTML from text files …

Источник

DOMDocument::loadHTML

The function parses the HTML contained in the string source . Unlike loading XML, HTML does not have to be well-formed to load. This function may also be called statically to load and create a DOMDocument object. The static invocation may be used when no DOMDocument properties need to be set prior to loading.

Parameters

Since Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

Return Values

Returns true on success or false on failure. If called statically, returns a DOMDocument or false on failure.

Errors/Exceptions

If an empty string is passed as the source , a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

Prior to PHP 8.0.0 this method could be called statically, but would issue an E_DEPRECATED error. As of PHP 8.0.0 calling this method statically throws an Error exception

While malformed HTML should load successfully, this function may generate E_WARNING errors when it encounters bad markup. libxml's error handling functions may be used to handle these errors.

Examples

Example #1 Creating a Document

See Also

  • DOMDocument::loadHTMLFile() - Load HTML from a file
  • DOMDocument::saveHTML() - Dumps the internal document into a string using HTML formatting
  • DOMDocument::saveHTMLFile() - Dumps the internal document into a file using HTML formatting

User Contributed Notes 19 notes

You can also load HTML as UTF-8 using this simple hack:

$doc = new DOMDocument ();
$doc -> loadHTML ( '' . $html );

// dirty fix
foreach ( $doc -> childNodes as $item )
if ( $item -> nodeType == XML_PI_NODE )
$doc -> removeChild ( $item ); // remove hack
$doc -> encoding = 'UTF-8' ; // insert proper

DOMDocument is very good at dealing with imperfect markup, but it throws warnings all over the place when it does.

This isn't well documented here. The solution to this is to implement a separate aparatus for dealing with just these errors.

Set libxml_use_internal_errors(true) before calling loadHTML. This will prevent errors from bubbling up to your default error handler. And you can then get at them (if you desire) using other libxml error functions.

When using loadHTML() to process UTF-8 pages, you may meet the problem that the output of dom functions are not like the input. For example, if you want to get "Cạnh tranh", you will receive "Cạnh tranh". I suggest we use mb_convert_encoding before load UTF-8 page :
$pageDom = new DomDocument ();
$searchPage = mb_convert_encoding ( $htmlUTF8Page , 'HTML-ENTITIES' , "UTF-8" );
@ $pageDom -> loadHTML ( $searchPage );

Pay attention when loading html that has a different charset than iso-8859-1. Since this method does not actively try to figure out what the html you are trying to load is encoded in (like most browsers do), you have to specify it in the html head. If, for instance, your html is in utf-8, make sure you have a meta tag in the html's head section:

If you do not specify the charset like this, all high-ascii bytes will be html-encoded. It is not enough to set the dom document you are loading the html in to UTF-8.

Warning: This does not function well with HTML5 elements such as SVG. Most of the advice on the Web is to turn off errors in order to have it work with HTML5.

If we are loading html5 tags such as

, there is following error:

DOMDocument::loadHTML(): Tag section invalid in Entity

We can disable standard libxml errors (and enable user error handling) using libxml_use_internal_errors(true); before loadHTML();

This is quite useful in phpunit custom assertions as given in following example (if using phpunit test cases):

// Create a DOMDocument
$dom = new DOMDocument();

// fix html5/svg errors
libxml_use_internal_errors(true);

// Load html
$dom->loadHTML(" ");
$htmlNodes = $dom->getElementsByTagName('section');

if ($htmlNodes->length == 0) $this->assertFalse(TRUE);
> else $this->assertTrue(TRUE);
>

Remember: If you use an HTML5 doctype and a meta element like so

your HTML code will get interpreted as ISO-8859-something and non-ASCII chars will get converted into HTML entities. However the HTML4-like version will work (as has been pointed out 10 years ago by "bigtree at 29a"):

It should be noted that when any text is provided within the body tag
outside of a containing element, the DOMDocument will encapsulate that
text into a paragraph tag (

).

For those of you who want to get an external URL's class element, I have 2 usefull functions. In this example we get the '

'
elements back (search result headers) from google search:

1. Check the URL (if it is reachable, existing)
# URL Check
function url_check ( $url ) <
$headers = @ get_headers ( $url );
return is_array ( $headers ) ? preg_match ( '/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/' , $headers [ 0 ]) : false ;
>;
?>

2. Clean the element you want to get (remove all tags, tabs, new-lines etc.)
# Function to clean a string
function clean ( $text ) $clean = html_entity_decode ( trim ( str_replace ( ';' , '-' , preg_replace ( '/\s+/S' , " " , strip_tags ( $text ))))); // remove everything
return $clean ;
echo '\n' ; // throw a new line
>
?>

After doing that, we can output the search result headers with following method:
$searchstring = 'djceejay' ;
$url = 'http://www.google.de/webhp#q=' . $searchstring ;
if( url_check ( $url )) $doc = new DomDocument ;
$doc -> validateOnParse = true ;
$doc -> loadHtml ( file_get_contents ( $url ));
$output = clean ( $doc -> getElementByClass ( 'r' )-> textContent );
echo $output . '
' ;
>else echo 'URL not reachable!' ; // Throw message when URL not be called
>
?>

Be aware that this function doesn't actually understand HTML -- it fixes tag-soup input using the general rules of SGML, so it creates well-formed markup, but has no idea which element contexts are allowed.

For example, with input like this where the first element isn't closed:

loadHTML will change it to this, which is well-formed but invalid:

Источник

Оцените статью