I was wondering if is it possible to change an already written variable in the html generated file. Maybe there is an option to 'publish/write' the html file in the very end of php processing.
<html>
<?php
echo '<h1>' . $pageTitle . '</h1>';
?>
[...]
<?php
[DB queries]
$pageTitle = "New Page title";
echo "<javascript-code-to-change-the-page-title>";
?>
Yes I could set $pageTitle before, but it may change along the code according to some query.
So, I figure out that I can change the page title on client side only.
I'm probably missing some logic here.
You could use a combination of (A) PHP's Output buffering, and (B) PHP's DomDocument class.
Basically, you would capture the HTML output by wrapping the output in ob_* commands. Once you get the output, you throw it in the DOM parser. Once you're there, you can then traverse the DOM document and make your changes. Afterwards, you can dump everything back out to the browser.
I most definitely would not rely on JS to do any changes on the page.
ALTERNATIVELY, you could do your PHP at the top of the file, then echo the variables out as needed.
Related
Let's say I have a file called 'file.php' in which is the following code:
<?php
$var = 'something';
echo '<p>' . $var . '</p>';
?>
Is it possible to delete these lines with PHP, without altering the rest of the file?
Once these lines have been output (sent to the browser) you will not be able to "delete" them solely using PHP.
You'll need to use a client-side scripting language such as JavaScript to get a handle on the <p> tag for example and then either hide it or remove it from the DOM.
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>";
So I would have a template file called template.php and inside, it would have:
<html>
some other content
<?php $name ?>
</html>
Essentially, I want to pull the name from the database and insert it into $name and save that entire page as a new file.
So let say in the database, I had a name called Joe, the new file I create would have the following content inside:
<html>
some other content
Joe
</html>
The only issue I am having is finding the right functions to actually replace the variable from the template file, and being able to save it into a new file.
That's not how templates usually work. You should have a template file, substitute variables for their values, and display it to the user. You can cache these generated templates (the ones that are always the same) but you never make a new template file for each user and save it permanently. That makes managing and updating the application way more difficult than you want.
I recommend using a simple template system like RainTPL. That's what I use for most of my projects... it's very simple to use and provides all the basic functionality you would need.
You can also use just PHP for this... there are a few answers on SO that cover this so I won't get into it.
Using PHP as template engine
Using Template on PHP
Look at the answer I gave to this question a while back. There I explain how "variable parsing" usually works.
When I create a template loader I generally use output buffering with php include. This allows you to run the page as a php file without displaying its content before you are ready. The advantage to "parsing" your php files this way is that you can still run loops and functions.
Here's an example of how to use output buffering with PHP to create what you're wanting.
The Template template.php
<html>
some other content
<?php $name ?>
</html>
Where your database code and stuff is index.php
<?php
$name = 'John Doe';
if( /* some sort of caching system */ ) {
$parsed_template = file_get_contents('parsed_template.html');
}else {
ob_start();
include 'template.php';
$parsed_template = ob_get_clean();
file_put_contents('parsed_template.html', $parsed_template);
}
echo $parsed_template;
?>
How can I replace the text on the active document body on a PHP file on code execution?
Do I have to import a file, replace text on it and then echo it or can I just manipulate the document I run the PHP script on?
I am trying to use templates for easier HTML editing like :usernamecomeshere: and then replacing that :usernamecomeshere: with the actual value. I am wondering If I can do it on one file only instead of loading a file and then displaying it.
If I'm getting your question correctly, you don't need to important a file and echo the document. You can directly manipulate the document itself. For example, in the below sample, you can directly echo the contents of $username in a way that's interspersed with HTML code.
index.php
<?php
// handle code to login
$username = "David";
?>
<html>
<body>
<p>Hello, your username is <?php echo $username ?></p>
</body>
</html>
Worth pointing out is that PHP itself is a templating engine. If you want to replace text, you can do it using PHP such as:
<?php
$user = 'Ugur';
?>
<html><head></head>
<body>
<h1>Hello <?php echo $user; ?></h1>
</body>
</html>
Beyond this sort of simple usage, you may want to look at various template engines, which allow you to do much more elegant things, but are more complex. Take a look at mustache, perhaps?
If you're trying to make these modifications after the page has loaded, remember that PHP runs on the server-side, not the user-side. For that, you need Javascript.
you'll want to look up str_replace() on google. You can search an entire string and replace specified keywords simply.
I am calling this function halfway down the page:
<div id="leftArea">
<?php
if (isset($id)){
single($id);
}
?>
</div>
The problem is i want use some of the output in the meta tags in the <head>, whats the best way to approach this?
EDIT:
The single function, takes the $id and echo's the data straight to the page, exactly where it is. In the DIV leftArea. But i want to take one of the rows from the DB and insert it into the META tags at the top
Copy the code into the <head> section.
Redesign your System
The best method is to create a class that manages your html page for you, example:
$Page = new HTMLPage("My Page",HTMLPage::Strict);
$Page->addScript("....");
$Page->addScript("....");
$Page->addScript("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->addStyle("....");
$Page->SetBody($MyTemplate);
$Page->send();
this way though out your functions you can do
function myfunc()
{
global $Page;
$Page->addScript("....");
}
the main point here is you should build your document up before sending it to the browser, this way you still have control over the content no matter where your code is executing from.
on the final send method you build your content up, and then push the content via echo, and then exit directly. (all processing should be done prior to output to manage errors)