This question already has an answer here:
Output code as text in php
(1 answer)
Closed 4 years ago.
I am using Sendgrid to send an email via PHP but I need to include a PHP script in the HTML content. I do not know how to perform the concatenation, it seems really delicate.
Here's my code:
$content = new SendGrid\Content("text/html", '
/*
some HTML content
*/
/*PHP script starts here*/
'.myPHP Script {
}//end of script
.'
/*some more HTML content
');
The '..' concatenation does not work and the entire page fails to load. How do I go about this? I am not trying to output the code. I want it to actually execute.
Don't make life difficult. What ever string you need to concat, put it in a variable and concat that. also note you can do string interpolation in PHP using the double quotes "$variable".
$concatHTML = '';
$prodList = [...]; // assume we have products list here
for ($i = 0, $max = count($prodList); $i < $max; $i++) {
$concatHTML += '<div>'. $prodList[$i] .'</div>';
}
$content = new SendGrid\Content("text/html", "
//some html content here
$concatHTML
");
EDIT
Concatenation could be done using the operator . (e.g.) echo 'hi ' . $name; where $name = 'some string' or using string string interpolation using double quotes and and placing (e.g.) echo "hi $name" you can add curly braces to make separate the variable from the other string like echo "hi Mr.{$name}".
For the php code you want to include in the email is probably not valid.
Related
This question already has answers here:
php curly braces replace values
(3 answers)
Closed 1 year ago.
i'm having a json file containing some strings for echoing.
Now i want to put variable contents between these strings e.g.:
$event_config = json_decode(file_get_contents("event_config.json"), TRUE);
$output_string = $event_config['e_header']['e_welcome_text'];
$name = value_lookup("f_personalien_vorname");
echo("Hello {$name},<br>Thanks. We have just received your query<br>");
echo ($output_string);
This one works fine, {$name} is replaced correctly with whats stored in $name.
echo("Hello {$name},<br>Thanks. We have just received your query<br>");
This version with the same string loaded from json isn't working. Instead of {$name} being replaced it just gets printed with the whole string.
echo ($output_string);
For reference my json currently looks like:
{
"e_header": {
"e_welcome_text": "Hello {$name},<br><br>Thanks. We have just received your query<br>",
"e_information": "Some string"
}
}
Does somebody have an idea about this?
Solved it.
Thanks to #El_Vanja giving me this hint.
I changed my json to:
{
"e_header": {
"e_welcome_text": "Hello {{name}},<br><br>Thanks. We have just received your query<br>",
"e_information": "Some string"
}
}
Note the double braces {{name}}.
Then came up with the following which is replacing the upper expression.
$output_string = $event_config['e_header']['e_welcome_text'];
echo(str_replace('{{name}}', $name, $output_string));
you should escape the quotes and append to have a variable pass.
$event_config = json_decode(file_get_contents("event_config.json"), TRUE);
$output_string = $event_config['e_header']['e_welcome_text'];
$name = value_lookup("f_personalien_vorname");
echo("Hello " . $name . ",<br>Thanks. We have just received your query<be>");
echo($output_string);
This question already has answers here:
concat a for loop - php
(3 answers)
Closed 2 years ago.
Am writing some content to a file, I store all the content in a variable. Some parts of the file content need to be repeated with some minor changes, those changes are stored in a variable and I need to run a for loop for that change.
See the following code sample,
$looped_valueArray; //this is an array i need to loop the content in $looped_value
//to show all the values
$content = 'sample content '. $looped_value.'
fdgdf';
I could not loop write a forloop appending to a string like
$content = 'sample content '.
foreach($looped_valueArray as $looped_value) $looped_value;.'fdgdf';
Hi follow this scripts if you would like append your content in a variable:
$content ="sample content ";
foreach($looped_valueArray as $looped_value){
$content .=$looped_value;
}
You can't use a loop directly in the string. Either define a separate function that loops through the values, or, in the above example, you could just use implode():
$content = 'sample content ' . implode("", $looped_valueArray) . 'fdgdf';
This question already has answers here:
How to echo a hyperlink with a variable? [closed]
(3 answers)
Closed 9 years ago.
I am a bit rusted in PHP and currently struggling to do something that should be quite simple.
$value = "PUPPY";
$html .= '<td>{$value}</td>';
echo $html; // Doesn't work, prints {$value}.... Should be... PUPPY
I know there's a simple way for doing this but i just forgot and cannot find a definitive method on Google or StackOverflow.
I need to place the html in a variable cause once it is all done, it gets used by tcpdf::writeHtml()
I already tried :
$html .= "<td align='right' nowrap>".$value."</td>";
echo $html; // But this outputs a TD tag with attribute nowrap="" and this is not valid HTML... tcpdf:writeHtml rejects this.
UPDATE: The bug was in fact caused by a bad usage of the "nowrap" attribute deprecated some time ago.
Single quoted strings are not interpolated in php. Use double quotes instead.
$value = "PUPPY";
$html .= "<td>{$value}</td>";
echo $html;
$value = "PUPPY";
$html .= "<td>{$value}</td>";
echo $html;
Use " instead of '
Take a look at the documentation about Strings on PHP
It should be
$html .= "<td>{$value}</td>";
Double quotes echo variables and single quotes do not.
Instead of '<td>{$value}</td>' put "<td>{$value}</td>".
It should work fine
I want to add code php to variable with html, for example
$html = '<b></b> <?php echo $lang["text"] ?>';
but it don't interpret php code. What am I doing wrong?
Use string concatenations like this:
$html = '<b></b>' . $lang['text'];
or insert variable in double quoted string like this:
$html = "<b></b>${lang['text']}";
both versions are correct, use the one that you like.
What you want is called string interpolation (read about how it works for PHP).
Your particular example would be solved using
$html = "<b></b> {$lang['text']}";
String interpolation only happens in double quoted string ("string here").
its very important to escape the output. (security basics)
$html = sprintf('<b>%s</b>', htmlspecialchars($lang['text']));
You can't switch from "Output raw text mode" to "Run PHP code mode" in the middle of a string while you are already in "Run PHP code mode"
$html = "<b></b> ${lang['text']}";
… although why you want an empty bold element is beyond me.
<?php
$html = '<b>'.$lang['text'].'</b>';
?>
make sure file extension is php.
<?php
$html = '<b>' . $lang["text"] . '</b>';
?>
Is there anyway to highlight HTML embedded in PHP single and double quotes — which has no scope defined, within Textmate?
Example:
printf( 'This is some <strong>Text</strong>', 'foobar' );
Everything within the single quotes belong to the same scope. Its annoying. Has anyone tried to fix this somehow? I'd rather not alter the language files (without guidance), im not fluent in regex.
Nevermind, i found out how to do it. After reading on how scopes work in Textmate. I opened up the PHP languages panel under the Bundle Editor and pasted an include of the following scope name into the string-single-quoted scope:
text.html.basic
Heres the include:
{ include = 'text.html.basic'; },
and heres how the entire string-single-quoted section looks like now:
string-single-quoted = {
name = 'string.quoted.single.php';
contentName = 'meta.string-contents.quoted.single.php';
begin = "'";
end = "'";
beginCaptures = { 0 = { name = 'punctuation.definition.string.begin.php'; }; };
endCaptures = { 0 = { name = 'punctuation.definition.string.end.php'; }; };
patterns = (
{ include = 'text.html.basic'; },
{ name = 'constant.character.escape.php';
match = '\\[\\'']';
},
);
};
You would probably have to do some rather complicated editing of the scope definitions for HTML in order to get this to work as you describe. PHP files are HTML files by default in TextMate, so you're looking to define a scope where you have an HTML file with embedded PHP with strings that might be HTML. Not every string in PHP is going to be HTML, so you'd need to figure out a way to differentiate non-HTML strings from HTML strings, and if you're not fluent with regular expressions, it would probably take quite a bit of research and trial and error to get it right.
As an alternative, if you're actually using printf rather than simply using it as a generic example, consider using the function to format only what you need formatted and doing something like this.
<?php
$var = sprintf( '%d', $num );
?>
Here's the <strong><?php echo $var; ?></strong>.