Sublime Text/代码解析错误无故,隐藏字符 [英] Sublime Text / code parse error for no reason, hidden characters

查看:459
本文介绍了Sublime Text/代码解析错误无故,隐藏字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我已经有很多年了,但是没有今天那么频繁.这促使我一劳永逸地问这个问题.

I have had this problem for years, but not as frequently as today. This prompted me to ask this question once and for all.

有时(今天几乎所有逻辑都如此),我琐碎的PHP代码由于解析错误而失败.有时PHP是正确的,但通常不会出错,并且再次编写完全相同的代码时,它不会发牢骚.复制和粘贴代码无效,必须再次完整键入.

Sometimes (today for almost every logic) my trivial PHP code fails with a parse error. Sometimes PHP is right, but way to often there is no mistake and when writing the exact same code again it works without whining. Copy and pasting the code does not work, it has to be typed again in full.

今天,我精确地键入了每个字符以使其与行失败完全相同,然后删除了旧字符,以使脚本正常工作.例如:

Today, I have meticulously typed down every character exactly as the line failing and then removing the old one, just to have the script working. For example:

1    foreach ($_POST['data_positions'] AS $k => $v) {
2    
3    }

在第1行上引发了解析错误.我将代码撞到了第2行,然后再次写入了完全相同的第1行(我多次检查了每个字符是否相同),然后删除了第2行,它开始工作.另一个例子:

threw a parse error on line 1. I bumped the code down to row 2 and wrote the exact same line 1 again (I check several times that every character is the same), then removing line 2, and it works. Another example:

1    if (is_numeric($k)) {
2    
3    }

在第1行上也失败.当我重新键入它时,它可以工作.重新复制并粘贴该行不起作用.

also fails on row 1. When I retype it, it works. Copy and pasting the line again does not work.

我100%确信代码是正确的,因此让我相信这是某种编码问题,并且代码的某些部分被归为不良编码(例如将格式化的文本粘贴到电子邮件中).

I am 100% certain the code is fine, so it leads me to believe it's some kind of encoding issue and that certain parts of the code is attributed a bad encoding (like pasting formatted text into a email).

我目前正在使用Sublime Text(但是在Notepad ++和旧的Homesite Editor中也有相同类型的错误解析错误),在OS X上运行MAMP,但是我的Windows台式机上也发生了这种情况.

I'm currently using Sublime Text (but have had the same types of mistaken parse errors in Notepad++ and the old Homesite editor as well), running MAMP on OS X, but it has happened on my Windows desktop machine as well.

有人知道我能做什么吗?多次编写代码并让代码随机出现在您身上真是太烦人了.

Does anyone have any clue what I can do? It's super annoying to write the code several times and having it fail on you randomly.

编辑(重新打开): 我仍然无法正常工作.无论是将其粘贴到记事本中还是从头开始键入一个全新的文件.我已经检查了从数据库到服务器再到编辑器的全部内容,它们都使用UFT-8.几乎总是会破坏IF语句(有时我必须将它们键入3到4次才能起作用.琐碎的内容,例如if ($a === 3 || $a === 6) {}.

EDIT (reopened): I still can't get it to work properly. No matter if I paste it to Notepad or if I type a completely new file from scratch. I've checked all through the database to the server to my editor that they all use UFT-8. It's almost always IF-statements that break (sometimes I have to type them down 3 or 4 times before it works. Trivial stuff, like if ($a === 3 || $a === 6) {}.

编辑(已解决!): 向下滚动以获取解决方案,我自己回答了问题.

EDIT (solved!): Scroll down for the solution, I answered the question myself.

推荐答案

我终于找到了答案.我必须通过反复试验找出我做错了什么.

I finally found the answer. I had to find out what I did wrong by trial and error.

因此,如果您的代码由于隐藏字符而中断,则在从UTF-8切换到ISO-8859-1时,如果发现字符为Â",则该情况主要发生在OSX和多个编辑器中(两者都发生)对我来说是Sublime Text和Coda),这可能是原因:

So, if your code breaks due to hidden characters, if you find out that the character is "Â" when switching to ISO-8859-1 from UTF-8, if it's mostly happening in OSX and in multiple editors (both Sublime Text and Coda for me) this is probably why:

在许多操作系统中,按alt+space键将获得正常空间的不间断空间.许多编程解析器(例如PHP,Xcode)在解析该字符时会失败.

In many operating systems you will get a non-breaking space instad of a normal space when you press alt+space. Many programming parsers (PHP, Xcode for example) will fail when parsing that character.

要在Sublime Text中对其进行修复,您需要编辑首选项/键绑定-用户并添加以下行:

To fix it in Sublime Text you need to edit Preferences/Key bindings - User and add the line:

{ "keys": ["alt+space"], "command": "insert_snippet", "args": {"contents": " " } },

请记住,键绑定文件是一个包含对象的数组,如果有更多条目,则需要将,保留在末尾.如果是最后一个,请将其删除.

Remember that the key bindings file is an array with objects, and you need to keep the , at the end if you have more entries. Remove it if it's the last one.

如果您需要在其他任何编辑器中对其进行修复,则只需使用Google的" yourEditor禁用替代空间".

If you need to fix it in any other editor, just Google for "yourEditor disable alt space".

我也注意到了与alt+shift+space相同的行为,因此我在键绑定配置中添加了另一行:

I also noticed the same behaviour with alt+shift+space, therefore I added another line to the keybinding config:

{ "keys": ["alt+shift+space"], "command": "insert_snippet", "args": {"contents": " " } },

:)

这篇关于Sublime Text/代码解析错误无故,隐藏字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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