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.
Related
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.
So I'm using Postmark to send emails and the class I have requires a variable as the message body, as below:
$email->to(Input::post('email'))->subject("Verify Your Email Address")->html_message($html)->send();
This works fine if I set $html as just plain html.
What I am trying to do is send the contents of another php file from my site as this html.
I have tried:
$Vdata = file_get_contents('verification.php');
this works fine but as soon as I try and pass variables in it gives me an error:
file not found error
And sends a blank email, for example:
$Vdata = file_get_contents('verification.php?url=blah');
Essentially I just need $html to be the contents of verification.php?url=blah so that I can pass in variables to that file.
Can anyone help?
You're doing a local file inclusion, which means filenames ONLY. URLs are not permitted (query strings in particular) because you're NOT doing an HTTP request. PHP is going to look for a file whose name literally contains ?, u, r, etc... which of course doesn't exist.
If you want to use query strings, then you have to use a full-blown absolute URL, including the protocol:
include('http://....?url=...');
However, this is incredibly inefficient, and also highly dangerous. Since you're now EXECUTING the file specified in the url. you're going to get its output, not the raw PHP code in the file.
If you want to pass data to an included file, then just variables:
$foo = 'bar';
include('test.php');
and use look for/use those variables in the file.
This is going to be hard to explain so please ask me for clarification if anything confuses you.
So let's say that I am on createpage.php. Within this page, I have the following tasks.
Create a new page (file) using PHP via $file = fopen($pagename . '.php', "x").
Add premade content to this new page.
The method that I am taking to add new content to the new page is by storing the HTML content code into one variable called $newpagecontent. Then, I simply use fwrite($file, $newpagecontent).
The problem is though, that you can't store the whole of HTML/PHP file into $newpagecontent without breaking the double quotes. For example, $newpagecontent="echo("hello!")".
So the question is, is there a better way to add in HTML content to a newly created page? or is there a way to store the HTML code into a variable without breaking the syntax?
Thanks a lot
If you just want the contents of the other file that contains the HTML/PHP code, then try file_get_contents to read the contents and store it in the variable. Please see the documentation of this function for further details.
I run 2 applications on my site and want to use that same template for both. My Joomla site stores it's template config in a params.ini file in the following manner:
sidebara_width=150
sidebarb_width=300
mainbody_width=500
each parameter in 1 line no commas or semicolon after that. I want to use the same values for my other template. like <div id="sidebars" style="<?php echo $sidebara_width ?>.px">
I need a small php script which can read these values from the params.ini file and assign a value of 150 to a variable called $sidebara_width, assign a value of 300 to a variable called $sidebarb_width and so on.
Kindly help
there is a function called
parse_ini_file
you could use it like this
$ini_array = parse_ini_file('path to file');
$sidebara_width = $ini_array['sidebara_width']
$sidebarb_width = $ini_array['sidebarb_width']
and so on
regards
You might want to checkout the parse_ini_file (http://php.net/manual/en/function.parse-ini-file.php)
Otherwise you can use preg_match_all (http://ca2.php.net/preg_match_all) and use a simple RegEx to match it.
Christian
Suppose you have access to a script which will print or echo an ID string, given a name string, i.e., something like:
http://www.example.com/script.php?name=aNameString
outputing an ID string.
I want to create a script which will allow me to retrieve anIDString, given that I already have a variable holding aNameString, i.e., something like this pseudocode:
$name="Homer Simpson";
$id='www.example.com/script.php?name=$name';
Can you help me understand how I'd do this? ... Thanks, as always!
If you are writing code on the same domain, for security reasons you might consider the include() or require() functions instead, and implementing what you need as a function in php. This way, there is no risk to your server being fed rubbish data and crashing your application.
If you need to pull data from another script do so with care, especially a server that isn't trusted. That said, you can do it with either: http://uk.php.net/curl or http://us2.php.net/manual/en/function.file-get-contents.php, the latter of which looks easier to me.
Try requiring the file, but remember, you'll need to call the function later.
<?php
$name = 'Homer Simpson';
require 'script.php';
?>
That will make the global variable $name, accessible by script.php
However, if it isn't your server, you will need to use a tool like curl to fetch the page.
In the simplest case, you can use the HTTP wrappers to get the output:
$html = file_get_contents('http://www.example.com/script.php?name=aNameString');
and them take the $html apart, unless you meant something different by "outputing an ID string", it output raw text and not html.