php 如何删除网页脚本中的 \ufeff 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18029992/
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 16:50:00  来源:igfitidea点击:

How do I remove a \ufeff character in my webpage scripts?

phphtmlutf-8

提问by J4Numbers

I am trying to, simply put, remove a hidden zero-width-line-break character (U+FEFF) in my script. The webpage that it has appeared on is at http://cynicode.co.uk(Please note, the index page has been tinkered with and is the only one which functions properly at the moment)

我试图,简单地说,在我的脚本中删除一个隐藏的零宽度换行符(U+FEFF)。它出现的网页是http://cynicode.co.uk(请注意,索引页已经过修补,是目前唯一可以正常运行的页)

By looking at the html elements on the page, this shows up:

通过查看页面上的 html 元素,可以看到:

enter image description here

在此处输入图片说明

The key point being the red dot between < body >and < !--5-- >. This, when hovered over, shows that it is a \ufeffcharacter. The problem is that, when I look through the script, there is no such character that exists.

关键点是< body >和之间的红点< !--5-- >。将鼠标悬停在上面时,这表明它是一个\ufeff字符。问题是,当我查看脚本时,不存在这样的角色。

I am using PHP and HTML to construct this page, and the items between < !--4-- >and < !--5-- >consists of the following. Firstly, on the actual index page itself:

我正在使用 PHP 和 HTML 来构建此页面,并且介于< !--4-- >和之间的项目< !--5-- >由以下内容组成。首先,在实际的索引页面上:

<?php
  echo "<!--4-->";
  echo "<head><meta charset='utf-8' /><link rel='shortcut icon' type='image/ico' href='./images/CyniCode.ico'>
    <title>CyniCode :: Index</title>
    <meta name='description' content='The Cynic's paradise! Home of Cynical.' />
    <meta name='author' content='Cynical' />
    <meta name='keywords' content='Cynical;Blog;Code' />
    <link type='text/css' rel='stylesheet' href='./css/mystyle.css' />
    <link rel='shortcut icon' type='image/ico' href='./images/CyniCode.ico'>
    <link href='http://fonts.googleapis.com/css?family=Muli' rel='stylesheet' type='text/css' />
    <script type='text/javascript' src='http://static.proofiction.net/jquery/jquery-1.9.1.min.js'></script>
    <script type='text/javascript' src='http://static.proofiction.net/jquery/loginwait.js'></script>
    <script type='text/javascript' src='http://static.proofiction.net/jquery/googleAnalytics.js'></script>
    <script type='text/javascript' src='./http://static.proofiction.net/jquery/jquery.bxslider.js'></script>
    <script type='text/javascript' src='./http://static.proofiction.net/jquery/jquery.bxslider.min.js'></script>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'><!--
      google_ad_client = 'ca-pub-xxxxxxxxxxxx';
      /* BiggerNavBox */
      google_ad_slot = '3977705372';
      google_ad_width = 300;
      google_ad_height = 600;
      //-->
    </script>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-xxxxxxxx-1', 'cynicode.co.uk');
      ga('send', 'pageview'); 
    </script>

    </head>";
  require_once './functions/page.php';

This constitutes for the Index page. The page.php script referenced is my cheat for setting the page up quickly and cleanly. However... there is a small remaining difference between the two comments on the page. This is the remaining difference between the two comments.

这构成了索引页面。引用的 page.php 脚本是我快速、干净地设置页面的秘诀。但是......页面上的两条评论之间存在微小的差异。这是两个评论之间的剩余差异。

<?php
  echo "<!--5-->";

Any help that anyone can offer would be much appreciated. All code pieces were direct copy and pastes from my scripts.

任何人都可以提供的任何帮助将不胜感激。所有代码片段都是从我的脚本中直接复制和粘贴的。

回答by Cthulhu

Some of your .phpfiles (probably ./functions/page.phpcontains Byte Order Mark. If you are using some IDE, check this file's encoding properties and with luck you will be able to remove it.

您的某些.php文件(可能./functions/page.php包含Byte Order Mark. 如果您使用的是某些 IDE,请检查此文件的编码属性,如果幸运的话,您将能够将其删除。

EditIf you use *nix, Elegant way to search for UTF-8 files with BOM?should help.

编辑如果您使用*nix,以优雅的方式搜索带有BOM 的UTF-8 文件?应该有帮助。

回答by sapeish

Here is a solution to a similar issue: Extra BOM character in HTML invalidating DOCTYPE tag

这是类似问题的解决方案:HTML 中的额外 BOM 字符使 DOCTYPE 标记无效

Basically the source of the issue are PHP files encoded in UTF8 with BOM, encoding them in UTF-8 without BOMsolves the issue.

基本上问题的根源是用 编码的 PHP 文件UTF8 with BOM,对它们进行编码可以UTF-8 without BOM解决问题。

Notepad++ UTF-8 encoding without BOM

Notepad++ UTF-8 编码,无 BOM