有没有更好的方法在 PHP 中编写 HTML 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4510137/
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
Is there a better way to write HTML strings in PHP?
提问by Hubro
I find myself writing a lot of functions in PHP that return HTML code. They may look something like this:
我发现自己在 PHP 中编写了很多返回 HTML 代码的函数。它们可能看起来像这样:
function html_site_head()
{
return
"
<div id=\"site_header\">
<div id=\"site_header_inner\">
<div id=\"site_header_logo\"></div>
<div id=\"site_header_countdown\">BARE XX DAGER IGJEN</div>
</div>
</div>
";
}
Now I can swear I've seen better ways to write long strings in PHP. I like python's way of doing it:
现在我可以发誓我已经看到了在 PHP 中编写长字符串的更好方法。我喜欢 python 的做法:
return """
<div id="site_header">
<div id="site_header_inner">
<div id="site_header_logo"></div>
<div id="site_header_countdown">BARE XX DAGER IGJEN</div>
</div>
</div>
"""
As you don't have to escape quotation marks. An alternative is using single quotation marks, but that means no using PHP variables directly in the string. I swear I've seen something like this in PHP:
因为您不必转义引号。另一种方法是使用单引号,但这意味着不能直接在字符串中使用 PHP 变量。我发誓我在 PHP 中见过这样的事情:
return <<<
<div id="site_header">
<div id="site_header_inner">
<div id="site_header_logo"></div>
<div id="site_header_countdown">BARE XX DAGER IGJEN</div>
</div>
</div>
Or something similar. Could someone refresh my memory on this?
或者类似的东西。有人可以刷新我的记忆吗?
Thanks
谢谢
采纳答案by Gumbo
PHP knows several kinds of syntax to declare a string:
' … '
" … "
<<<DELIMITER … DELIMITER
nowdoc syntax(since PHP 5.3.0)
<<<'DELIMITER' … DELIMITER
So you don't have to use the double quotes per se.
所以你不必使用双引号本身。
回答by GoreDefex
If you need a method that can be used to store HTML into a string, this is the way to do it.
如果您需要一种可用于将 HTML 存储到字符串中的方法,这就是这样做的方法。
I would not use any of these other suggested methods lol, I would use an Output buffer. These other methods all seem mega dirty (because at scale there would be a lot of special characters flying around)... so at the risk of offending someone i'll just say the reasons why I like my way.
我不会使用任何其他建议的方法,哈哈,我会使用输出缓冲区。这些其他方法看起来都非常肮脏(因为在规模上会有很多特殊角色飞来飞去)......所以冒着冒犯别人的风险,我只会说我喜欢我的方式的原因。
If you use an output buffer like I have done...
如果您像我一样使用输出缓冲区...
When using an IDE you can still have great intelisense help with HTML and PHP separate from one another and still code out generic DOM Objects quickly.
使用 IDE 时,您仍然可以在 HTML 和 PHP 相互分离方面获得很好的智能感知帮助,并且仍然可以快速编码出通用 DOM 对象。
When viewing the code it looks a lot more human readable.
查看代码时,它看起来更具人类可读性。
The HTML code can be passed in its DOM ready state rather than as a string which will eventually need to be parsed and eventually created into a DOM ready state.
HTML 代码可以以其 DOM 就绪状态传递,而不是作为最终需要解析并最终创建为 DOM 就绪状态的字符串。
When using HTML as a string method it can get really rough trying to use the various string function search and replace methods in order to change some of your code after runtime. For example looking for the <img>
tag within a massive string of other HTML elements can be daunting especially when trying to find stuff between tags...
当使用 HTML 作为字符串方法时,尝试使用各种字符串函数搜索和替换方法以在运行后更改某些代码会变得非常粗糙。例如,在<img>
一大串其他 HTML 元素中查找标签可能会令人生畏,尤其是在尝试在标签之间查找内容时......
I would use the Output Buffer Stream like so...
我会像这样使用输出缓冲流......
ob_start();
?>
<div id="site_header">
<div id="site_header_inner">
<div id="site_header_logo"><?php echo $PhpVar; ?></div>
<div id="site_header_countdown">BARE XX DAGER IGJEN</div>
</div>
</div>
<?php
$output = ob_get_clean();
ob_flush();
return $output; OR return echo $output
回答by AmdY
Better way - don't write spaghetti code, use template or
更好的方法 - 不要编写意大利面条式代码,使用模板或
// php code
?>
<div id="site_header">
<div id="site_header_inner">
<div id="site_header_logo"><?=$echoPhpVar?></div>
<div id="site_header_countdown">BARE XX DAGER IGJEN</div>
</div>
</div>
<?php
//php code
回答by Tarek Kalaji
Sometimes for some reason is happens that PHP or Javascript or some naughty insert a lot of backslash. Ordinary function does not notice that. Therefore, it is necessary that the bit "inflate":
有时因为某些原因会发生 PHP 或 Javascript 或一些顽皮的插入大量反斜杠。普通函数没有注意到这一点。因此,有必要让位“膨胀”:
<?php
function removeslashes($string)
{
$string=implode("",explode("\",$string));
return stripslashes(trim($string));
}
/* Example */
$text="My dog don\\\\\\\\'t like the postman!";
echo removeslashes($text);
?>
RESULT: My dog don't like the postman!
This flick has served me very well, because I had this problem before.
这部电影对我很有帮助,因为我以前遇到过这个问题。
Source: here
来源:这里
回答by Matteo Italia
What you're looking for is heredoc syntaxor nowdoc syntax.
您正在寻找的是heredoc 语法或nowdoc 语法。
回答by bhamby
You can use a HEREDOC:
您可以使用 HEREDOC:
<?php
function blah() {
$bar = <<<EOS
<div id="site_header">
<div id="site_header_inner">
<div id="site_header_logo"></div>
<div id="site_header_countdown">BARE XX DAGER IGJEN</div>
</div>
</div>
EOS;
return $bar;
}
?>
回答by Skilldrick
As Haim said - heredoc
s are the way to go.
正如海姆所说 -heredoc
是要走的路。
If you find yourself writing single lines, remember (or realise) that you can use single quotes for HTML attributes (don't believe anyone that tries to tell you they aren't valid).
如果您发现自己在写单行,请记住(或意识到)您可以对 HTML 属性使用单引号(不要相信任何试图告诉您它们无效的人)。
So, e.g.:
所以,例如:
$html = "<div id='blah'>$contents</div>";
回答by Raftalks
I like to suggest this PHP library that can handle generating just about any HTML tag with php and uses closure nesting which helps readability as well.
我喜欢推荐这个 PHP 库,它可以处理用 php 生成几乎任何 HTML 标签,并使用闭包嵌套,这也有助于提高可读性。
https://github.com/raftalks/Form
https://github.com/raftalks/Form
for example - a table inside a div tag
例如 - div 标签内的表格
Html::make('div', function($html))
{
$html->table(function($table)
{
$table->thead(function($table)
{
$table->tr(function($tr)
{
$tr->th('item');
$tr->th('category');
});
});
$table->tr(function($tr)
{
$tr->td()->ng_bind('item.name','ng-bind');
$tr->td()->ng_bind('item.category','ng-bind');
$tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.
});
$table->setClass('table');
});
$html->setClass('tableContainer');
});