HTML string to PHP variable without loosing IDE syntax highlighting - php

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

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.

Related

Render html to page from database PHP [duplicate]

How would one go about showing PHP code on user end. Sort of like w3School does?
Having lets say a grey area div, and then showing the code in there without activating it?
You can use html entities <?php in the html it will be rendered as <?php
You can use htmlspecialchars to encode your code to use html entities.
Use <pre> or <code> tags to wrap your code.
Take a look at http://php.net/manual/en/function.highlight-string.php to further see how you can make the code look pretty.
Since passing a large block of code to highlight_string() can be messy, you may want to look at output buffering in combination with highlight_string to output colorized php code.
Something like:
<?php
ob_start();
?>
phpinfo();
echo "this echo statement isn't executed";
<?php
$code = ob_get_clean();
highlight_string($code);
?>
Simply you can use following code to display php code on webpage.
highlight_string("<?php print('This is php code.'); ?>");
It will give output like
<?php print('This is php code.'); ?>
The first step is to not wrap that code in PHP tags. So instead of this:
<?
var sample = "code";
?>
You would have this:
var sample = "code";
It's not the code itself which triggers the server-side compile from the PHP engine, it's the tags which indicate to that engine what blocks of the file are code and what are not. Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.
Once you're outputting the code, it's then a matter of formatting it. The old standard is to wrap it in pre tags to get rid of HTML-ish formatting:
<pre>
var sample = "code";
</pre>
You can also apply CSS style to the pre tags (or any other tags you want to use for displaying code, such as div) as you see fit.
There are also very useful code syntax highlighting plugins and tools to make the code a lot "prettier". Google-code-prettify often comes highly recommended.
Typically this is done by showing code within <pre> or <code> tags.
You can use this template........
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
It will show the source code and output following.
use the header function of php, this will rea
<?php
header("content-type: text/plain");
?>
The PHP code will just be a string that you can echo or print onto the page, no different than any other data you want PHP to display for you. If you want to keep the formatting (ex. the indentation), put it inside a <pre><code> block.
Ex:
$php_code = '<?php $foo = bar; ?>';
echo "<pre><code>$php_code</code></pre>";

PHP Concatenate file output to string

I am rewriting a piece of a program to change it from using "echos" throughout the script to create a large output variable using Heredocs instead, which outputs at the end of the file.
A piece of the script includes another PHP file that directly outputs HTML and has php logic within the HTML it outputs. This file is used by other pieces of the overall program that are not yet being rewritten (due to time constraints).
Is it possible to append the output of another file to an $output variable? I've tried doing this, but it it doesn't work for string appending:
$output .= include 'foo.php';
$output .= file_get_contents('foo.php');
The file_get_contents wrote all the PHP logic directly in HTML, as I suspected it would and the straight 'include' echo'd the HTML as I also expected.
Is there a method to get the output buffer of the file and append to a string?
EDIT: Nevermind the question, I completely forgot about OB_Buffering. Added an answer with my solution, no need to answer this one
I feel stupid. I found the answer 5 minutes after posting, I completely forgot about ob_buffering:
ob_start();
include('./foo.php');
$output .= ob_get_contents();
ob_end_clean();

Use PHP to echo whats inside div tags

I dont know what to research or where to start here.
What im trying to do is use PHP to read an HTML Page and pull out the raw text contained inside a div
the div is this
<div class="thingy">
test
</div>
When the php is executed, I want it to echo
Test
Is there an easy snippet for this, or can someone post a small script?
Edit: the html page with the Div is on another webpage.
What you're looking to do is parse HTML. Use the DOM module that comes with PHP to do this: http://php.net/manual/en/book.dom.php
You do NOT want to try to do this with regular expressions.
If you want to remove ALL the HTML tags from a document, use the PHP strip_tags() function: http://us3.php.net/strip_tags
While this could possibly be done using regex, I would recommend using a DOM parser. My reccommendation goes to SimpleHTML Dom Parser. Using it, here's how you would do what you want
$string = "<div class=\"thingy\">test</div>";
$html = str_get_html($string); // create the DOM object
$div = $html->find('div[class=thingy]', 0); // find the first div with a class of 'thingy'
echo $div->plaintext(); // echo the text contents
If you want to parse your html you can use it like
<?php
$str = '<div class="thingy">test</div>';
echo strip_tags($str);//OUTPUT : test
?>
As your html is on other webpage, start output buffering include that file in your main php script, do all manipulation on it to get the content.

How to clear all elements in a page with PHP?

Is there any way to clear all html elements in a php page?
For example I have 100 html elements in my page, is there anyway to remove them?
As we know with javascript we have innerHTML but in PHP what?
clear all html elements in a php page
That doesn't make sense. HTML elements only exist in the DOM after PHP has executed and sent an HTML document to the browser. Server-side, where PHP executes, there are no elements to remove.
If you're trying to manipulate the HTML you've already output, you need to capture it with output buffering (see ob_start, ob_get_contents and ob_end_clean) but if your goal is to "clear all html elements", presumably so you can output a different set of elements, you simply need to not output anything in the first case. If this sounds like what you're trying to accomplish, you need to look into simple conditional statements like if/else.
as we know with javascript we have innerHTML but in php what ?
There is no PHP-equivalent because PHP doesn't have access to the client-side DOM. It is purely a server-side technology, and the output of your PHP script is the input to the browser. The DOM and its elements are generated long after your PHP script has executed. If you have an XHTML fragment in a string, and you want to parse/manipulate it, you can use xpath.
If your question is "clear html elements in a php file", the answer is: strip_tags().
$string = '<p>hello</p>';
echo strip_tags($string);
Try this:
<?php
if(//why you want to clear the elements){
echo "<script language=\"javascript\">";
?>
//Append all elements in <div id="body">
var body = document.getElementById("body");
body.innerHTML ="";
<?php
echo "</script>";
#Output your new element
echo "New elements.";
}
?>
Try this, it should definitely work.
<?php
echo "<script>document.write('');</script>";
?>

php: file_get_contents is stripping out php code

I'm attempting to file_get_contents and output php code as a string without being rendered. The idea is to grab the raw un-rendered file contents so they can be edited in a textarea...
// file "foo.php" I'm needing the contents of
<h1>foo</h1>
<? include 'path/to/another/file' ?>
// php file that's calling the file_get_contents
<?php
echo file_get_contents('foo.php');
?>
The above code is stripping out the php include in foo.php which outputs:
<h1>foo</h1>
Does anyone know how I can get foo.php contents as a raw un-rendered string where output will be?:
<h1>foo</h1>
<? include 'path/to/another/file' ?>
Any help is greatly appreciated!
As far as I know you can't get php content unless it's on the same server.
Make sure you're trying to access a locally hosted file and not something remote and it should work.
Also if you try to echo code it will try to parse it, so pass it through htmlspecialchars($source) and it should work.
Something like this:
<?php
echo "<pre>";
echo htmlspecialchars(file_get_contents('file.php'));
echo "</pre>";
?>
Would echo formatted source code of the php file, including comments and any other text in it without being parsed. And since it looks like it's important to you, I'd also say that it shows in the DOM of course since it's no longer code, now it's text. You can place it inside a container, style it and do whatever you want with it.
You can also do :
<?php
highlight_file('file.php');
// or alternatively
echo highlight_file('file.php',true);
And that will output the file like with htmlspecialchars and file_get_content but within <code> tags and with some syntax highlighting.
highlight_string :
(PHP 4, PHP 5, PHP 7)
highlight_string — Syntax highlighting of a string
highlight_file :
(PHP 4, PHP 5, PHP 7)
highlight_file — Syntax highlighting of a file

Categories