Double string into variable - php

Basic question, I'm sure this has been answered before. But since I'm not familiar with PHP I don't know what to search for, hence the title of this question.
This code outputs 'my custom field value'
$title = stripslashes($_POST['user-submitted-customfield']);
I'd like to make it 'my custom value from #mysite', something like
$title = stripslashes($_POST['user-submitted-customfield']) + from + #mysite;
which I tried and returned an error

concat string in PHP
USING .
stripslashes($_POST['user-submitted-customfield']).' from #mysite';
from php.net
There are two string operators. The first is the concatenation
operator ('.'), which returns the concatenation of its right and left
arguments. The second is the concatenating assignment operator ('.='),
which appends the argument on the right side to the argument on the
left side. Please read Assignment Operators for more information.

$title = stripslashes($_POST['user-submitted-customfield']).'from'.'#mysite';

If you need a complete string you have to concat the strings with . and you have to set your words in quotes.
$title = stripslashes($_POST['user-submitted-customfield']).' from #mysite';

PHP is different from javascript, we use dots to concatinate strings and strings must be quoted:
$title = stripslashes($_POST['user-submitted-customfield']) . "from #mysite";

There are many ways to achieve that result. Some of them are:
$user_feedback = stripslashes($_POST['user-submitted-customfield']);
$title = $user_feedback . ' from #mysite';
echo $title;
echo '<br>';
$title = "$user_feedback from #mysite";
echo $title;
echo '<br>';
$title = "{$user_feedback} from #mysite";
echo $title;
echo '<br>';
$template = ':user_feedback from #mysite';
$title = str_replace(':user_feedback', $user_feedback, $template);
echo $title;
echo '<br>';
$template = '%s from #mysite';
$title = sprintf($template, $user_feedback);
echo $title;
echo '<br>';

Related

PHP prevent html entity creation at string concatenation

How can i prevent that PHP converts a recognized part of a string to an html-entity?
So e.g. lets say i have to concat parts together to an url, like:
echo '&' . 'section=' . '<br>';
$a = '&a';
$b = 'mplitude=';
echo "{$a}{$b}" . '<br>';
echo sprintf("%s%s", '&quote', '=');
the code above prints the following:
§ion=
&litude=
"e=
instead of:
&section=
&amplitude=
&quote=
how can this be prevented without throwing filters on it trying to convert the symbols back to an string again?
You need using htmlspecialchars function:
echo htmlspecialchars('&' . 'section=' . '<br>');

php echo string html with php-variables, can't break the php string

I'm trying to echo a phpstring-message. This php string consists of html and php variables and comes form a database and i can't change that data.
$name = 'John';
$str = '<b>Hi {$name},</b><br/>How are you?';
echo $str;
So i'm trying to replace the php string, but it doesn't work. This is my code:
$str = str_replace('{', '\' . ', $str);
$str = str_replace('}', ' . \' ', $str);
I get: <b>Hi' . $name. ',</b><br/>How are you?
How do i get the string like this?
<b>Hi John,</b><br/>How are you?
Thank you in advance
Just do it like this, you won't be able to replace it with a concatenation:
echo str_replace('{$name}', $name, $str);
EDIT:
If you don't know the name of the variable just use this:
echo preg_replace('/\{(.*?)\}/', $name, $str);
it's already implemented in PHP, you can directly write the variable in double quote like this:
echo "<b>Hi $name,</b><br/>How are you?";
or for some more complex variables:
echo "<b>Hi {$user->name},</b><br/>How are you?";

Calling PHP function from within a string

I can't find a solution how to combine single and double quotes to echo HTML and call a function at the same time:
foreach ($result as $r) {
echo "<a href='get_permalink(get_page($r->id))'>".get_permalink(get_page($r->id)).'</a><br>';
}
Problem is this part is parsed as text, not php
"<a href='get_permalink(get_page($r->id))'>"
Cansome one help me to combine this? get_permalinks and get page are wordpress built in functions, so they should have function behavior
You can't call a function inside double " quotes.
foreach ($result as $r)
{
echo "<a href='".get_permalink(get_page($r->id))."'>".get_permalink(get_page($r->id)).'</a><br>';
}
It's not possible to run PHP code when it's inside a string (unless with eval). However, you can use printf() to separate code from string:
$url = get_permalink(get_page($r->id));
printf('%1$s<br>', htmlspecialchars($url, ENT_QUOTES, 'UTF-8'));
The %1$s is a positional format specifier; this is done so that the encoded $url value only has to be passed once.
Just concat the string like this:
echo "<a href='". get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . "</a><br>";
Also if you want to know what's the difference between single and double quotes see this:
What is the difference between single-quoted and double-quoted strings in PHP?
try this way:
if($result as $r)
{
echo "<a href='" . get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . '</a><br>';
}

PHP tags and quotes in variables causing syntax/parse errors

These lines are causing a syntax/parse error, and I'm not sure how to fix it. I'm pretty sure it's either the <?php and ?> tags, or the single quotes. Any ideas?
$data = '<?php
$title = "'. $name. '";
$keywords = "'. $title. ','. $developer. '";
$description = "How to install '. $title. ' for PC and Mac.";
include('templates/modheader.php');
include('templates/modfooter.php');
?>';
Thanks in advance.
include('templates/modheader.php');
include('templates/modfooter.php');
is the culprit: You mix single quotes. SImply use
include("templates/modheader.php");
include("templates/modfooter.php");
include('templates/modheader.php');
include('templates/modfooter.php');
should be
include("templates/modheader.php");
include("templates/modfooter.php");
For large blocks of string data, I highly recommend the HEREDOC / NOWDOC syntax. You can combine that with sprintf to inject values
$template = <<<'_PHP'
<?php
$title = '%s';
$keywords = $title . ',%s';
$description = "How to install $title for PC and Mac.";
include 'templates/modheader.php';
include 'templates/modfooter.php';
?>
_PHP;
$data = sprintf($template, $name, $developer);

Syntax and Variables in PHP

I'm having some trouble with writing some syntax. I want to echo this
'location_1' => $location_1,
However, it is not as simple as it seems. When I write the echo statement the integer 1 must be the variable $z. Here is the code I attempted to write
echo "'location_' . $z . '' =>' . ${'location_' . $z} . ','";
This is what it outputted
'location_' . 1 . '' =>' . something . ','
$location_1 is equal to the string something. I'm lost at how to do this the right way. Any guides on describing how this syntax works would be a major help too so I can understand it completely.
You can just write variables directly into double quoted strings see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
echo "'location_$z' => \$location_$z,";
You might want to also read the rest of the strings doc
This is the link to the echo documentation (see the examples, I think they described well how it works)
You can break it into two lines and get the expected output.
For example:
$var_location = "$". "location". $z;
echo "'location_" . $z . "' =>'" . $var_location . "','";
One way is: echo "'location_{$z}' => \$location_{$z},";
Edit: Is this what you meant?
<?php
$z = 1;
$location_1 = 'something';
echo "'location_$z' => " . ${'location_'. $z} . ',';
which produces: 'location_1' => something,
Why don't you store these variables inside an array for easier access. Something like:
$locations = array('location_id' => 'location_name');
Here's one way:
echo "'location_$z' => \$location_$z,";
You need to escape the $ symbol. The double quotes represent the thing to echo in this case, whereas the single quotes actually get echoed.

Categories