PHP 在 require 上添加了额外的空格 [英] PHP adds extra whitespace on require

查看:34
本文介绍了PHP 在 require 上添加了额外的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

search.phpcategory.php 本质上是相同的结构——一个包含一些特定内容的 div 容器.这里没什么特别的,纯 HTML:

<!-- 废话-->

然而,当用require_once(或require/include等)插入时,PHP会在每个元素上方添加空格,将其向下推,可识别作为 Chrome 的 Inspect Element 工具中的空文本节点(删除此节点时,空格会消失)

从侧边栏脚本中删除所有不必要的空格(使其成为一行代码)并不能解决问题.如果我只是用组件的内容替换 require_once 行,则不会出现空格.所以不知道为什么 PHP 在 require 上添加它.有什么想法吗?

更新

这仍然被证明是一个奇怪的.我现在同意 require_once 似乎不是根本原因.我决定暂时忽略这个问题,并希望在我进一步研究之后它会消失.唉,它仍然存在,所以我做了更多的调查.检查浏览器中的页面源代码确认有问题的代码确实作为一个长完整的行返回 http://pastebin.com/dtp7QNbs - 任何标签之间没有空格或回车,但浏览器中会出现空格 - 在检查元素工具中可识别为每个 <div class="component"> 之间的空行.

这是否有助于进一步阐明这个问题?

解决方案

问题解决了!这花了很长时间才弄清楚.简短的回答是我的 php 文件是 UTF-8 编码的.在 Notepad++ 中将其更改为 ANSI 修复了它.

我只是通过对输出 HTML 进行逐个字符的比较来找到问题的真正原因 - 一个是使用require_once"的地方的输出,另一个是手动粘贴代码的地方.

在输出的视觉比较中,两者看起来相同 - 长度相同,没有额外/不同的字符.但是当通过 preg_split('//', $string) 并逐个字符循环时,在每个 require_once 的开头显示了 3 个额外的不可见"字符插入点.我将它们识别为 ASCII 字符 &#239;&#187;&#191;(双点i,一个正确的 V 形和一个倒置的问号).

将编码更改为 ANSI(当我在记事本中逐字重新创建其中一个脚本时,我发现这是原因,并且没有遇到同样的问题),多余的行已经消失了.

Consider the following code:

<div id="sidebar">
<?php
  require_once('/components/search.php');
  require_once('/components/categories.php');
?>
</div>

search.php and category.php are essentially the same structure - a div container with some specific contents. Nothing special here, pure HTML:

<div class="component">
<!-- blah -->
</div>

However, when inserted with require_once (or require / include etc), PHP adds whitespace above each element, pushing it down, identifiable as an empty text node in Chrome's Inspect Element tool (the whitespace disappears when this node is deleted)

Deleting all unnecessary whitespace from the sidebar script (making it a single line of code) doesn't fix it. And if I just replace the require_once lines with the contents of the components, the whitespace doesn't appear. So not sure why PHP is adding it on require. Any ideas?

Update

This one's still proving to be a weird one. I agree now that require_once does not seem to be the root cause as such. I decided to ignore the problem for a while and hope it would go away after I'd worked on it further. Alas, it remains, so I did bit more investigating. Checking the page source in the browser confirms that the code in question is indeed returned as a single long unbroken line http://pastebin.com/dtp7QNbs - there's no whitespace or carriage return between any of the tags, yet space appears in the browser - identifiable in the Inspect Element tool as empty lines between each <div class="component">

Does this help shed any more light on the issue?

解决方案

Problem solved! This took forever to figure out. The short answer is that my php files were UTF-8 encoded. Changing this in Notepad++ to ANSI fixed it.

I only found the real cause of the problem by doing a character-by-character comparison of the output HTML - one output from where 'require_once' was used and one where the code was manually pasted in place.

In a visual comparison of the output, both appeared identical - same length, no extra/different characters. But when pushed through preg_split('//', $string), and looped through character by character, 3 extra "invisible" characters were revealed at the start of each require_once insert point. I idenitified these as the ASCII characters &#239;, &#187; and &#191; (a double-dotted i, a right chevron and an upside-down question mark).

Changed the encoding to ANSI (I discovered this as the cause when I recreated one of the scripts in Notepad word-for-word and it did not suffer the same issue), and the extra lines have gone.

这篇关于PHP 在 require 上添加了额外的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆