Printing PHP var in PHP string var - php

I'm trying to print out the value of a variable within another string.
Here is what I'm trying to do:
$output = '<section class="cards cards--$alignment background--black">'
$alignment has the value of left or right. The above renders when inspecting:
<section class="cards cards--$alignment background--mint">
I have also tried:
$output = ' <section class="cards cards--'.$alignment.' background--black">'
But this renders:
<section class="cards cards-- background--black">
And have also tried:
$output = ' <section class="cards cards--{$alignment} background--black">'
But this renders:
<section class="cards cards--{$alignment} background--mint">
I have tried all methods I've seen on other SO questions, but can't get it printing the value?

This code of yours should work
$output = ' <section class="cards cards--'.$alignment.' background--black">'
If it doesn't then the $alignment variable is just empty or doesn't exist, try an "echo $alignment" before your $output line to see if you get something.

Related

PHP: put a variable inside a variable, or edit the variable

I'm pretty new to PHP, got a question, I can't figure out after few research.
Here is my sample code
<?php
$sample = '<div class="sample"><ul><li>123</li><li></li></ul></div>';
$newsample = '<span class="newsample">11</span>';
/* Some code here to put the $newsample variable inside $sample */
?>
My expected output is like below:
<div class="sample">
<li>123</li>
<li><span class="newsample">11</span></li>
</div>
Is there anyway we can do that?

Show a HTML code in a HTML page using PHP nl2br OR str_replace does not work

Please help tried all ways nothing worked. I am inserting some code to MySQL DB using PHP 7 and after insert the code which is all fine; thus:
Eg:
<p>Hello:<b>Amber</b>\r\n<div>\r\n<hr>\r\n\r\n<button onclick=\"insertTextAtCursor()\">Lisa sisend</button>
When I call the record set using PHP in the HTML this is NOT showing anything since the HTML codes are hidden.
My Need is to show the HTML code in Plain text BUT "without \r\n shown Line Breaks" - formatted; thus:
Eg:
<p>Hello:<b>Amber</b>
<div>
<hr>
<button onclick=\"insertTextAtCursor()\">Lisa sisend</button>
Pure css and javascrpt code's are shown nicely with this PHP lines but NOT the HTML:
$author_code1 = null;
$author_code1 = $row["author_code1"];
$author_code1 = str_replace('\r\n', '<br />', $row['author_code1']);
echo $author_code1;
Got closer to it adding thus:
<?php
echo "<xmp>";
echo str_replace("\r\n", "<br/>", $author_code1);
echo "</xmp>";
?>
This shows all in one line:
</html>\r\n </textarea>\r\n</div>\r\n </div>\r\n</section>\r\n<button>Lisa send</button>
My Question is why isn't it breaking to the next line? Like:
<div>
<p>Hello:<b>Amber</b></p>
</div>
<hr>
<button>Lisa send</button>
Please help.
I thought it provide answers to a Large extent. Many just reply and some copy paste. However, I found a solution:
$author_code1 = null;
$author_code1 = $row["author_code1"];
$author_code1 = str_replace('\r\n','', $author_code1);
$author_code1 = str_replace('<',"<", $author_code1);
$author_code1 = str_replace('>',">", $author_code1);
$author_code1 = str_replace('</',"</", $author_code1);
<pre>
<code class="html hljs">
<!-- my db code -->
<?php echo $author_code1; ?>
<!-- my db code -->
</code>
</pre>

Insert image into html code variable

I have a PHP variable like this:
Example1
$data = '<p>This is paragraph.</p>
<p title="not-special">No!</p>
';
Example2
$data = '<p>This is paragraph.</p>
<p title="special">Yes!</p>
';
I want to have a php function, that checks whether there's p element with title="special", and if so, insert div element "YAP!".
How can this be done?
You can accomplish it in various different ways, a working one could be:
$data = '<p>This is paragraph.</p>
<p title="special">Yes!</p>
';
if (strpos($data, 'special') !== false) {
$data = '<p>This is paragraph.</p>
<p title="special">Yes!</p>
<div>YEP</div>
';
}
echo $data;
Anyway, this was not the best way to ask a question, please have a look at: How to ask a question
You could use PHP Function to check if the variable contains a certain string, see: http://php.net/manual/de/function.strpos.php
If there is a special case for formatting in a view, you can specify it easily with a variable.
<?php
// Check somewhere for the special case
$specialCase = $_GET['success'];
?>
<p>This is paragraph.</p>
<p title="special">Yes!</p>
<?php
// Display the extra div.
if ($specialCase === 'true') {
echo '<div class="yap"></div>';
}
<?php

Turning a <div> content into a $variable in php

I have this website were you can order products.
The title of the products you can order are in HTML:
<div class="whatever"> Title </div>
I want to retrieve this "title" and set my php variable $product to the value "Title".
I have search a lot on the internet but somehow I am not able to find my answer.
How can I do it?
You can use \DOMDocument->loadHTML();
Ex:
<?php
$doc = new \DomDocument();
$doc->loadHTML('<div class="whatever"> Title </div>');
View examples here:
http://docs.php.net/manual/en/domdocument.loadhtml.php
This is assuming that your source is available to php. It would probably be more pragmatic to extract the value with javascript in the client and send it with the page request. If your app is well structured, the logic that renders the title into the page in the first place is probably where you should be looking to retrieve the information rather than trying to parse the html separately.
If you mean that you would like to do this from the client side, you should be using AJAX to achieve this. However, I think you mean that you want to put the HTML in a variable. That is very simple:
$variable = "<div class=\"whatever\"> Title </div>";
And to output the HTML:
echo $variable;
You can also add multiple elements to a single variable by concatting.
$variable = "";
$variable .= "<div class=\"whatever\"> Title </div>";
$variable .= "<div class=\"whatever\"> Another Title </div>";
echo $variable;
If you mean that you want to echo a variable within a dv, that works exactly the same way:
<div class="title"><?php echo $product; ?></div>
Or better looking:
<div class="title"><?= $product; ?></div>

PHP: How can I put HTML Code with PHP Code within into a PHP Variable?

I have a question about " and ' in PHP. I have to put a complete <li> element into a PHP variable but this doesn't work, the output is completely false...
$list =
"
<li class=\"<?php if($info[1] < 700) {echo \"half\";} else {echo \"full\";} ?>\">
<div class=\"onet-display\">
<?php if ($image = $post->get('image.src')): ?>
<a class=\"onet-display-block\" href=\"<?= $view->url('#blog/id', ['id' => $post->id]) ?>\"><img class=\"onet-thumb\" src=\"<?= $image ?>\" alt=\"<?= $post->get('image.alt') ?>\"></a>
<?php endif ?>
<h1 class=\"onet-thumb-title\"><?= $post->title ?></h1>
<div class=\"uk-margin\"><?= $post->excerpt ?: $post->content ?></div>
</div>
</li>
";
Is it because there is PHP Content in the HTML Code? How can I solve this?
Can someone help me and explain why this doesn't work?
<?php ... <?php
Since your string contains PHP tags, I suppose you expect them to be evaluated. The opening PHP tag within another PHP tag is interpreted as a part of the PHP code. For example, the following outputs <?php echo time();:
<?php echo "<?php echo time();";
There are several ways to build a PHP string from PHP expressions.
Concatenation
You can create functions returning strings and concatenate the calls to them with other strings:
function some_function() {
return time();
}
$html = "<li " . some_function() . ">";
or use sprintf:
$html = sprintf('<li %s>', some_function());
eval
Another way is to use eval, but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior.
Output Buffering
If you are running PHP as a template engine, you can use the output control functions, e.g.:
<?php ob_start(); ?>
<li data-time="<?= time() ?>"> ...</li>
<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;
Output
<li data-time="1483433793"> ...</li>
Here Document Syntax
If, however, the string is supposed to be assigned as is, use the here document syntax:
$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;
or
$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;
You want to store some html into a variable.
Your source should (if not yet) start with
<?php
Then you start building the contents of $list.
Starting from your code the nearest fix is to build $list by appending strings:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "\"half\""; // RESULT: <li class="half"
}
else
{
$list .= "\"full\""; // RESULT: <li class="full"
}
// ...and so on...
Now a couple things to note:
$list .= "... is a short form of $list = $list . "...
Where the . dot operator joins two strings.
Second thing you may make code easier to read by mixing single and double quotes:
Use double quotes in PHP and single quotes in the generated HTML:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "'half'"; // RESULT: <li class='half'
}
else
{
$list .= "'full'"; // RESULT: <li class='full'
}
// ...and so on...
This way you don't need to escape every double quote
i think you have to work like this
$test = "PHP Text";
$list = "<strong>here ist Html</strong>" . $test . "<br />";
echo $list;

Categories