functions/SkriptParser.php
<?php
$text = file_get_contents(basename($_SERVER['PHP_SELF']));
preg_match_all("/{(.*?)}/", $text, $matches);
var_dump($matches[0]);
echo str_replace($matches[0],"Test",$text);
?>
Is my current code, this is called from my index page which is here:
index.php
require_once 'functions/SkriptParser.php';
When i open index.php it replace the correct strings [ It'll replace any string inside {} however, it seems to be replacing <?php and I have no clue why.
Any ideas?
I'm not sure I fully understand what you are trying to do.
This line $text = file_get_contents(basename($_SERVER['PHP_SELF']));resolves to a local filename which will be the name of the script it's included in.
It won't load the page which that script would output, it will load the file without executing the code. The contents of $text will be a string containing the pure php content.
When I run your code from the command line and use "Here is a test of {your code}" as my string the output is:
{your code}<?php
require_once 'includetest.php';
print_r(basename($_SERVER['PHP_SELF']));
echo "Here's a test of Test";
If I run it from a browser and view the source I see that too, but the browser escapes the php so it isn't rendered on the page. So (for me at least), your concept kinda works. However, the fact the code isn't executed means it will never do what you intend.
Here's what I don't really understand though. In essence within index.php what you are telling your code to do is load a script which will load another copy of index.php and replace content within it.
If you want to change the behaviour of index.php then alter the code within index.php, don't generate unsuitable output and then load another script in an attempt to parse it before returning it. Just output it the way you want the first time.
Where is the content within the {} that you want to remove coming from? Why do you want/need to remove it? If you can clarify exactly what your aim is then it will be easier to suggest a solution.
Related
I am trying to pass specific variables to the html file which has extension of .php
for e.g I am trying to load main.php by using loadHTMLFile() function in another file. but when I try to add code like this
"<?php echo "hello world"?>"
and running the website and checking it's inspect elemnt this php code is commented, do you have any idea or way around of this?
I have tried using semicolon like this
'.<?php echo "hello world" ?>.'
also
'.echo "hello world".'
but it does not works
function loadHtml($loadfile){
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($loadfile);
echo $doc->saveHtml() ;
}
and is called in index.php like this:
loadHtml("./views/html/shop/main.php");
about errors there are no errors just that I can't call the php variable or function or anythin php related to the main.php file because anything that has php tags around it is commented. Please help me if you know solution or way around this.
You can use file_get_contents() with eval()
Example: eval(file_get_contents('file.php'));
But you will either have to remove the PHP tags from the file you want to include or do like this: eval('?>'. file_get_contents('file.php'));
Why you didn't use just require("./views/html/shop/main.php") or include("./views/html/shop/main.php")?
Using eval() is not great idea, because it call script in special context, and is it unsafe, less effective to power and little hard to debug errors in future (stack trace log is not contain source of eval files).
I am using the following code:
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try one of the navigation links above or a search? Or it is possible you are trying to access a restricted page without being logged in to gain access.', 'shapely' ); ?></p>
Which ends up being displayed as follows:
It looks like nothing was found at this location. Maybe try one of the navigation links above or a search? Or it is possible you are
trying to access a restricted page without being logged in to gain
access.
What I want to display is this:
It looks like nothing was found at this location.
Maybe try one of the navigation links above or a search?
Or it is possible you are trying to access a restricted page without
being logged in to gain access.
So what I want is a way to break the line. I tried <br /> and \n, and neither work. Is there a way to add line breaks in the esc_html_e() function?
I know this a older question, I wanted to to point out that there is no need to use the output buffering functions in PHP. You can simplify this code by calling the WordPress function esc_html__() instead of esc_html_e(). This function will return the escaped value instead of echoing it out.
https://developer.wordpress.org/reference/functions/esc_html__/
echo nl2br( esc_html__("Line1\nLine2\nLine3", 'shapely') );
First off you can't use \n inside single quotes. Also, line breaks won't be rendered as such on on a webpage. From the looks of things esc_html_e() actually echoes the output so to capture & process it you'd need to do something like this:
ob_start();
esc_html_e("Line1\nLine2\nLine3");
$output = ob_get_contents();
ob_end_clean();
echo nl2br($output);
But this seems like an awful lot to go through and probably the wrong way to go about it. If you need to output html you probably don't be using esc_html_e() to begin with. Really hard to say without more context.
Is there a way to perform search and replace on PHP code before it is interpreted by PHP engine ?
Desired timeline:
PHP code is <?php echo("hello"); ?>.
Search and replace operation is hello → good bye
PHP code is now <?php echo("good bye"); ?>.
PHP engine interprets the code (output is good bye).
It is possible to manipulate the output of the PHP engine, using ob_start or even mod_substitute as an output filter of Apache. However, is it possible to manipulate the input of the PHP engine (PHP code, request, etc)?
Edit:
I'm working with thousands of PHP files and I don't want them to be modified. I would like to replace host1 with host2 in these files, since the files were copied from host1 and they have to be executed on host2. Indeed, several tests are made on the host name.
You could use a php script that opens a .php file, makes the necessary replacement, then includes that file.
Request is unusual)
Looks stupid. But if you really need it try this..
For example you need to change file test.php before execute it.
Try following steps (in index.php for example):
1) Open test.php for reading and get its content.
2) Replace everything you want.
3) Save changed text as test2.php
4) Include test2.php.
EDIT
Better thin why you need it? Try using variables.
It will help)
You should study template engines like smarty and various forms of prefilters.
Modifying code like this isn't good idea, however to answer your question... You may load original code, preprocess it and store in new file:
$contents = file_get_contents( 'original.php');
// ...
file_put_contents( 'new.php', $contents);
include( 'new.php')
But I don't see any valid use of this...
You also may possibly use eval() but every time you do that a kitty dies (no really, it's dangerous function and there's really just a few valid uses of it).
I am a newbie coder trying to build a simple web app using PHP. I am trying to send an HTML email that has a variable that will change each time it is sent. The code to initiate the email is 'email.php' and contains:
$body = file_get_contents('welcome/green2.html.php');
Within the 'green2.html.php' file, I have a variable called $highlight that needs to be populated. The $highlight variable is defined within the 'email.php' file. I had tried to simply add within the 'green2.html.php' file, however it is not being parsed. I get a blank space where the variable should be when it is output.
Also, I have done an include 'welcome/green2.html.php' within the 'email.php' file. When I echo it, the $highlight var is shown on the resulting page, but not if I echo $body.
Any help would be much appreciated!
Have you tried the str_replace function? http://php.net/manual/en/function.str-replace.php.
Add a placeholder in HTML (for instance #name# for name, #email# for email), and then use the string replace function once you've loaded the content of the file.
$bodytag = str_replace("#name#", $name, $myfile);
Loading a file via file_get_contents() will not cause it to be parsed by PHP. It will simply be loaded as a static file, regardless of whether it contains PHP code or not.
If you want it to be parsed by PHP, you would need to include or require it.
But it sounds like you're trying to write a templating system for your emails. If this is what you're doing, you'd be better off not having it as PHP code to be parsed, but rather having placeholder markers in it, and then using str_replace() or similar functions to inject variables from your main program into the string.
Hope that helps.
Use http://php.net/manual/en/function.sprintf.php put a %s in your code instead of the variable read the content and put the string into the sprintf with the variable you want to put that's it. Hope this will help.
I have a strange behavior with PHP system function. In this php script there are only 2 instructions (the rest being pure html):
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
<?php echo system('cgi-bin/gallery2/galleryview.cgi'); ?>
The first cgi just returns a single line as you can check here
http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi
It returns
My Gallery
But the whole php script returns My Gallery Twice:
My Gallery
My Gallery
http://reboltutorial.com/gallery2.php
Is there a reason (I don't use My Gallery in second cgi script of course see http://reboltutorial.com/cgi-bin/gallery2/galleryview.cgi) and how to prevent this ?
Thanks.
Update: The system function will do two things. The first is, it will run a command and pass its output through to the browser and/or output buffer. The second is, it will return the last line of output. So when you're saying
echo system('/...');
You're saying "Hey system, output the results of this command" and then "Hey ehco, output whatever system returns". Removing the echo
system('/...');
will fix your problem.
A few other things to check
Are you sure its galleryheaderview.cgi that's returning things twice? Comment out the include to make sure its actually the script that's echoing My Gallery twice
Is your PHP page/program included/constructed in such a way that galleryheaderview.cgi is being called twice?
Are you sure that calling the URL http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi is calling the same command line as cgi-bin/gallery2/galleryheaderview.cgi?
If you've checked out the three items above, you'll need to drop into the source of galleryheaderview.cgi and see why its outputting the header twice.
Are you absolutely sure that nothing else is outputting the My Gallery before this line? You should try removing it, and see if it goes completely away or if there is still is one "My Gallery"
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
If this doesn't bring you any further, maybe you have included some php file twice?