user2012141
user2012141

Reputation: 23

Using PHP variables in HTML when the HTML is in a PHP variable

I've got a .php script. In a variable I have a added html, but in that html I want to get text from another variable, how do I do this?

This is the variable with html:

$ai_boc   = '
<html>
<head>
  <title>Notes by Douno</title>
</head>

<body>
<div align="center">
<div style="width:600px; height:auto; border-radius:10px; background-color:#F7F1B8; background-repeat: auto; font-family:Georgia;  color:#513414;">

    <div style="height:100px; padding-top:20px; padding-left:20px; text-align:left;">
    <p style="margin-bottom:0px; font-weight:bold; font-size:xx-large;">$ai_su</p>
    <p style="margin-top:5px; font-size:x-small; font-weight:bold;">$ai_da</p>
    </div>
        <div style="height:1px; background-image: url(http://dl.dropbox.com/u/99775621/divider.jpg); display:block; position:relative;">
        </div>
    <div style="position:relative; overflow:hidden;">

        <div style="height:auto; width:375; padding-left:20px; text-align:left; position:relative; float:left;">
            <div style="width:375px;"> <p style="font-size:x-small; font-weight:bold;"><?php echo $ai_bo?></p>
            </div>
        </div>

        <div style="height:auto; width:160px; padding-left:20px; padding-right:20px; text-align:left; position:relative; float:left;">
        <p style="font-size:x-small; font-weight:bold; vertical-align:bottom;">If this email is considered spam, please contact us.</br></br>This email was send via Notes. An Android app developed by Douno.</p>
        <a href="https://play.google.com/store/apps/details?id=appinventor.ai_L_Bogaerts_95.Notes">
        <img alt="Get it on Google Play"
        src="https://developer.android.com/images/brand/en_generic_rgb_wo_45.png" />
        </a>

        </div>

    </div>
        <div style="height:1px; background-image: url(http://dl.dropbox.com/u/99775621/divider.jpg); display:block; position:relative;">
        </div>
    <div style="height:100px; width:600px; display:inline-block; position:relative;">

    </div>      






</div>
</div>
</body>
</html>
';

I coudn't find any working explanation, so I hope someone here can help me further. The html is not finished... Still some changes I'm gonna make.

Upvotes: 0

Views: 1338

Answers (9)

Nick
Nick

Reputation: 602

You can use something like this:

$test = 'test';
$html = '<h1>' . $test . '</h1>';

or

$test = 'test';
echo "Hello {$test}";

another way (available only in PHP 5.3+):

    $test = 'test';
    $html = <<<EOT 
Hello $test
EOT;

Upvotes: 2

SDC
SDC

Reputation: 14212

If you must embed large block of HTML in your PHP code, then the HEREDOC syntax described in @Kalpesh Mehta's answer is the way to do it.

However, the best answer here is to use a proper templating mechanism - even it's just a basic string-replace of a predefined marker. Then you can split your HTML out to a separate file, which will make it much easier to work with than having it embedded in a PHP string.

So template.html contains your code, with markers like {marker} where you want the variables.

Then your PHP code can just look like this:

$ai_boc = file_get_contents('template.html');
$ai_boc = str_replace('{marker}', $my_variable, $ai_boc);
print $ai_boc;

Upvotes: 0

Yukul&#233;l&#233;
Yukul&#233;l&#233;

Reputation: 17102

When you have a long html string you can use ob_start()and ob_get_clean() :

<?php
ob_start();

$title = "My website";

?><html>
    <head>
        <title><?=$title?></title>
    </head>
    <body>
        ...
    </body>
</html><?php

$ai_boc = ob_get_clean();

Upvotes: 0

Krynble
Krynble

Reputation: 624

There are a few options.

PHP understands quotes in different ways (read this, should clarify your doubt: http://php.net/manual/en/language.types.string.php)

Lastly, this is how it should work:

$replacement = "This is a string I want to insert into another one";
$bigger_text = "This is a bigger text. {$replacement} And the former variable has just been inserted here";

This happens because PHP "parses" double quotes and interpolates variables when needed.

It's also a good practice to brace your interpolated strings with braces {} for clarity's sake and also allowing you to interpolate things like this:

$interpolated = "Hello {$user[0]['name']}.";

Upvotes: 0

user2021438
user2021438

Reputation:

Your main quotes should be double quotes, the you can say

$ai_boc="<html ... {$variable} ... more html";

Upvotes: 0

Mithun Sen
Mithun Sen

Reputation: 523

try to add this to your code -

$ai_boc .= $other_var;

Upvotes: 0

Kalpesh
Kalpesh

Reputation: 5695

Use HEREDOC .. You can have HTML and PHP variables inside Heredoc like you would generally do it.

$ai_boc   = <<< EOT
<html>
<head>
  <title>Notes by Douno</title>
</head>

<body>
<div align="center">
<div style="width:600px; height:auto; border-radius:10px; background-color:#F7F1B8; background-repeat: auto; font-family:Georgia;  color:#513414;">

    <div style="height:100px; padding-top:20px; padding-left:20px; text-align:left;">
    <p style="margin-bottom:0px; font-weight:bold; font-size:xx-large;">$ai_su</p>
    <p style="margin-top:5px; font-size:x-small; font-weight:bold;">$ai_da</p>
    </div>
        <div style="height:1px; background-image: url(http://dl.dropbox.com/u/99775621/divider.jpg); display:block; position:relative;">
        </div>
    <div style="position:relative; overflow:hidden;">

        <div style="height:auto; width:375; padding-left:20px; text-align:left; position:relative; float:left;">
            <div style="width:375px;"> <p style="font-size:x-small; font-weight:bold;"><?php echo $ai_bo?></p>
            </div>
        </div>

        <div style="height:auto; width:160px; padding-left:20px; padding-right:20px; text-align:left; position:relative; float:left;">
        <p style="font-size:x-small; font-weight:bold; vertical-align:bottom;">If this email is considered spam, please contact us.</br></br>This email was send via Notes. An Android app developed by Douno.</p>
        <a href="https://play.google.com/store/apps/details?id=appinventor.ai_L_Bogaerts_95.Notes">
        <img alt="Get it on Google Play"
        src="https://developer.android.com/images/brand/en_generic_rgb_wo_45.png" />
        </a>

        </div>

    </div>
        <div style="height:1px; background-image: url(http://dl.dropbox.com/u/99775621/divider.jpg); display:block; position:relative;">
        </div>
    <div style="height:100px; width:600px; display:inline-block; position:relative;">

    </div>      






</div>
</div>
</body>
</html>
EOT;

Upvotes: 1

One Man Crew
One Man Crew

Reputation: 9578

Exapmle:

$var="Test";

$html="<html> $var </html>";

OR

$html="<html>". $var." </html>";

Upvotes: -1

Bart Friederichs
Bart Friederichs

Reputation: 33563

Use either string concatenation, or variable expansion:

 $html = '<html>'.$somevar.'</html>';      // concatenation
 $html = "<html>$somevar</html>";           // expansion, only works with double quotes (")

But much better is to use a decent template system to separate HTML and PHP altogether.

Upvotes: 2

Related Questions