PHP 中空格字符的转义序列
目前我正在使用下面的代码片段,它通过使用几个空格字符来缩进生成的 HTML:
add_filter('get_search_form', 'filter_search_form');
function filter_search_form($form) {
$form = ' <form role="search" method="get" id="searchform" action="' . home_url('/') . '"><input type="text" placeholder="' . __('Search') . '" value="' . get_search_query() . '" name="s" id="s"><input type="submit" id="searchsubmit" value="' . esc_attr__('Search') . '"></form>' . "\n";
return $form;
}
现在我一直在阅读一些有关空白字符的内容(\t
表示制表符,\n 换行符等),但我不完全确定在这种情况下如何实现这一点。 我尝试过使用
\s
来表示单个空格,但到目前为止没有任何运气。
作为 PHP 的新手,我希望您能提供帮助(最好不要使用“常规”空格字符)。
Currently I'm using the below snippet, which indent the resulting HTML by using several space characters:
add_filter('get_search_form', 'filter_search_form');
function filter_search_form($form) {
$form = ' <form role="search" method="get" id="searchform" action="' . home_url('/') . '"><input type="text" placeholder="' . __('Search') . '" value="' . get_search_query() . '" name="s" id="s"><input type="submit" id="searchsubmit" value="' . esc_attr__('Search') . '"></form>' . "\n";
return $form;
}
Now I've been reading some about whitespace characters (\t
for tab, \n
for newline, etc.), but I'm not entirely sure how to implement this in this situation.
I've tried using \s
for a single space, but without any luck thusfar.
Being relatively new to PHP, I hope you could assist (preferably without using a 'regular' space character).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 http://php.net/manual/en/regexp.reference.escape .php,一般的十六进制转义序列
\x20
应该可以工作,\040
(八进制)也应该可以工作。但就我个人而言,我认为以这种方式指定空格并没有多大(如果有的话)好处,因为它会使您的代码可读性较差,恕我直言。只需在单引号或双引号内添加文字空格(就像现在一样)即可完成。
或者,如果您尝试使用空格来缩进生成的 HTML 代码(正如您所看到的那样),以
\t
为单位这样做并不是世界末日。According to http://php.net/manual/en/regexp.reference.escape.php, the general hexadecimal escape sequence
\x20
should work, as should\040
(octal).Personally, though, I don't see much (if any) benefit to ever specifying spaces in this manner, as it would make your code less readable, IMHO. Just stick literal spaces inside your single- or double-quotes (like you have now) and be done with it.
Alternatively, if you're trying to use whitespace to indent the resulting HTML code (as it seems you are), doing so in units of
\t
isn't the end of the world.\t
和\n
等字符需要用双引号引起来...$string = "\t" 。 '<表单>';
Characters like
\t
and\n
need to be in double quotes...$string = "\t" . '<form></form>';
如果你想在它之前插入一个制表符,你可以使用:(
不要忘记双引号,单引号不能与 \t、\n 等一起使用!)
如果你想要空格,就使用空格!
它是 html 代码,因此它们将出现在您页面的源代码中。它们不会“塌陷”成一个空间。
If you want to insert a tab before it you could use:
(don't forget the double quotes, the single ones don't work with \t, \n and friends!)
If you want spaces, just use spaces!
It's html code, so they will be present in the source code of your page. They won't ‘collapsed’ into a single space.