HTML 到 PHP 变量(PHP 代码之外的 HTML)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1581586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:10:04  来源:igfitidea点击:

HTML into PHP Variable (HTML outside PHP code)

phpoutput-buffering

提问by Maksim Vi.

I am new to php and wondering if I can have something like this:

我是 php 的新手,想知道我是否可以有这样的东西:

<?php
 ...
 magicFunctionStart();
?>

<html>
   <head>...</head>
   <body>...</body>
</html>

<?php
 $variable = magicFunctionEnd();
 ...
?>

What I have to use right now is

我现在必须使用的是

<?php
 ...
 $variable = "<html><head>...</head><body>...</body></html>"
?>

Which is annoying and not readable.

这很烦人且不可读。

回答by Wabbitseason

Have you tried "output buffering"?

您是否尝试过“输出缓冲”?

<?php
 ...
 ob_start();
?>

<html>
   <head>...</head>
   <body>...<?php echo $another_variable ?></body>
</html>

<?php
 $variable = ob_get_clean();
 ...
?>

回答by eplawless

I think you want heredocsyntax.

我想你想要heredoc语法。

For example:

例如:

$var = <<<HTML
<html>
   <head>
random crap here
</html>
HTML;

回答by CMS

I'm not really sure about what you are trying to accomplish, but I think something like the heredoc syntaxmight be useful for you:

我不太确定你想要完成什么,但我认为像heredoc 语法这样的东西可能对你有用:

<?
$variable = <<< MYSTRING

<html>
   <head>...</head>
   <body>...</body>
</html>

MYSTRING;

However if you are trying to make HTML templates I would highly recommend you to get a real templating engine, like Smarty, Dwooor Savant.

但是,如果您正在尝试制作 HTML 模板,我强烈建议您使用真正的模板引擎,例如SmartyDwooSavant

回答by Toby Allen

Ok what you want to do is possible in a fashion.

好的,您想要做的事情以某种方式是可能的。

You cannot simply assign a block of HTML to a php variable or do so with a function. However there is a number of ways to get the result you wish.

您不能简单地将 HTML 块分配给 php 变量或使用函数来分配。但是,有多种方法可以获得您想要的结果。

  1. Investigate the use of a templating engine (I suggest you do this as it is worth while anyway). I use smarty, but there are many others
  2. The second is to use an output buffer.
  1. 调查模板引擎的使用(我建议你这样做,因为它无论如何都是值得的)。我使用smarty,但还有很多其他的
  2. 第二种是使用输出缓冲区。

One of the problems you have is that any HTML you have in your page is immediately sent to the client which means it cant be used as a variable in php. However if you use the functions ob_start and ob_end_fush you can achive what you want.

您遇到的问题之一是页面中的任何 HTML 都会立即发送到客户端,这意味着它不能用作 php 中的变量。但是,如果您使用函数 ob_start 和 ob_end_fush,则可以实现您想要的。

eg

例如

<?php 
  somesetupcode();
  ob_start();  ?>
<html>
<body>
html text
</body>
</html>
<?php
  //This will assign everything that has been output since call to ob_start to your    variable.
  $myHTML = ob_get_contents() ;
  ob_end_flush();

?>

Hope this helps you can read up on output buffersin php docs.

希望这有助于您阅读php 文档中的输出缓冲区

回答by T.Todua

I always recommend to AVOIDbuffer functions (like ob_start,or etc) whenever you have an alternative (because sometimes they might conflict with parts in same system).

每当您有替代方案时,我总是建议避免使用缓冲函数(如ob_start, 或等)(因为有时它们可​​能与同一系统中的部件发生冲突)。

I use:

我用:

function Show_My_Html()
{ ?> 
    <html>
      <head></head>
      <body>
         ...
      </body>
    </html>
    <?php 
}


...
//then you can output anywhere
Show_My_Html();

回答by trainmania100

$html_content = '
    <p class="yourcssclass">Your HTML Code inside apostraphes</p>
';
echo $html_content;

回答by gfsd4gs56g14sd56

Its REALLY CRAZY but be aware that if you do it :

它真的很疯狂,但请注意,如果您这样做:

<?php echo ""; ?>  

You will get it:

你会得到的:

<html><head></head><body></body></html>  

Keep calm, its only php trying turn you crazy.

保持冷静,它唯一的 php 尝试让你发疯。