Remove multiple lines from file with PHP - php

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.

Related

php post-processing publish

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.

str_replace (or another option) for replacing content located inside a php document

I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.

How to display PHP & HTML source code on a page?

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>";

Drupal: Print field without markup

Is there a way to print field content without getting all the markup? I'm new to Drupal, but I'm aware of the field.tpl.php, however, I'm just wondering if there's a quicker way to get the content in a node--custom.tpl.php. It would compare to Wordpress's <?php echo get_field('field_name'); ?>
Well, apart from using field.tpl.php, I can think of 2 solutions:
first:
Use a php snippet to strip html tags in your template.php.
in your template.php
function mytheme_strip_html_tags($n_field) {
return preg_replace("/<.*?>/", "", $n_field);
}
then call the function mytheme_strip_html_tags($field_name)
if you use several themes, however, you need to copy this snippet to each one of them.
EDIT: You can make a module and place that snippet inside. This way it works with every theme.
second:
Download the tokens module. Tokens are references to your fields. Tokens module have a output mode that strips html for you. [field_name-raw]
You need to follow instructions in how to add tokens, but is not that difficult.
You have access to the $node variable inside a node.tpl.php, so:
<?php print $node->field_monkey_height; ?>
should work... note that many fields will hide their data inside arrays (for multiple value fields, etc) so you may need to do a bit of:
<?php drupal_set_message(print_r($node->field_monkey_height), 1); ?>
...to figure out the exact path to the data you need.
You simply can use PHP's strip_tags() like so:
<?php print strip_tags($node->field_name[LANGUAGE_NONE][0]['value']); ?>

PHP file print its own content

Is it possible for PHP file to print itself, for example <?php some code; ?> that I get output in HTML as <?php some code; ?>(I know its possible in c++), if not is it possible to actually print html version of php code with nice formatting and colors such as from this url inside code container http://woork.blogspot.com/2009/07/twitter-api-how-to-create-stream-of.html. OR from this website when you press code, while posting your example your code gets wrapped or whatever term is for that, makes it distinguishable from other non-code text. tnx
Yes.
<?php readfile(__FILE__)
__FILE__ is a magic constant that contains the absolute filesystem path to the file it is used in. And readfile just reads and prints the contents. And if you want to have a syntax highlighted HTML output, try the highlight_file function or highlight_string function instead.
I'm not sure if this is exactly what you want but you can print a file using:
echo file_get_contents(__FILE__);
or syntax-highlighted:
highlight_file(__FILE__);

Categories