Dan Hastings
Dan Hastings

Reputation: 3280

HTML string to PHP variable without loosing IDE syntax highlighting

I want to add a very large HTML string to a PHP variable. When i do something like $html = "<div>info</div>"; the string will go gray and the normal highlighting no longer works. I want to use some PHP to build the HTML, but most of it will be coded directly into the file. I can't echo the HTML it needs to be in a variable as it gets passed to a function.

Is there another way other than $html = ""; to assign data to a variable that will allow me to keep syntax highlighting. My thoughts would be some sort of syntax that will allow me to close the PHP tag, but won't output the content, but rather saves that output to a variable.

?$html>
<div>content</div>
<?php

I understand this is impossible as the server will not read any lines outside of the PHP tags, but it's just an example to get across what I'm trying to do.

Edit I have also thought of using

$html = file_get_contents(site.com/file.php);

This would be wasteful as it creates another HTTP request to a PHP page. the page needs to be PHP in order to dynamically build some of the HTML

Upvotes: 2

Views: 109

Answers (1)

trincot
trincot

Reputation: 350760

You can use output buffering with ob_start and ob_get_clean for that:

<?php
ob_start();
?>
<html>
... all your html comes here
</html>
<?php
$html = ob_get_clean();
?>

At the end of the above code, nothing will have been output to the browser, but your $html variable will have the content.

Upvotes: 1

Related Questions