简体   繁体   中英

How do i add a string to a data type that had html?

I'm creating a share page for my website. I want the user to be able to send a message along with the formatted html.

so I have.

$message = $_REQUEST['message'];

$page = 'some html code';

mail($TO,$subject,$page, $headers);

I want the $message and $page to be added together.

You can do $message .= $page;

It is equivalent to "append $page to $message "

If you just want do concatinate the two strings?

$body = $message . $page;
mail($TO,$subject,$body, $headers);

Example taken from http://php.net/manual/en/language.operators.string.php

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

I would strongly advise you to sanitize the user input beforehand.

just do the following it should work:

$message = $_REQUEST['message'];

$message .= 'some html code';

mail($TO,$subject,$page, $headers);

The . is a concatenation operator and you can read more about it here.

I use heredoc to accomplish what (I think) you're trying to do. The example might look like

$message = "I'm a message";

$page = <<<HTML
<p>I'm a $message that's been inserted into html.</p>
HTML;

Like other users have said before me, never, ever use input directly from a user without first sanitizing or filtering it.

to answer the question here...

Well that doesn't seem to be working. How can I insert $message into the html? – creocare

    $template = '<html>
            <body>
            <h1>A message from something.com</h1>
            <p>
            {{userMessage}}
            </p>
            </body
            </html>';

$userMessage = 'hey there im the message';
$message = str_replace( '{{userMessage}}', $userMessage, $template );
echo $message;

you can get you dynamic message into your html email template using str_replace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM