Table of Contents
回复内容:
转义和转义不一样
什么转义都不能省略
我赞成的做法
如何对付特殊符号?
Home Backend Development PHP Tutorial php中特殊字符用html代码好还是转义好?

php中特殊字符用html代码好还是转义好?

Jun 06, 2016 pm 08:47 PM
php

嗯,因为是纯小白,这个问题可能有点菜,各位大神见谅……
就是在写php时,需要echo特殊字符,比如"这个符号,写入php时是用转义\"好还是用html代码"好呢?
请教各位,谢谢!

谢谢各位!已经大致明白了,非常感谢各位的耐心解答!
PS.php大神果然不一样,回答都是那么的详细~~在此感谢了!

回复内容:

嗯,因为是纯小白,这个问题可能有点菜,各位大神见谅……
就是在写php时,需要echo特殊字符,比如"这个符号,写入php时是用转义\"好还是用html代码"好呢?
请教各位,谢谢!

谢谢各位!已经大致明白了,非常感谢各位的耐心解答!
PS.php大神果然不一样,回答都是那么的详细~~在此感谢了!

要明白哪个好的话首先我们就要搞清楚两者之间的区别:

  • 使用转义的话就是相当于输出原字符,既然特殊字符原字符输出了的话就必须和页面的编码方式和浏览器的编码方式有关系。如果页面的编码(如GBK)中不包含该特殊字符的话,或者浏览器的编码方式不包含该特殊字符的话,就会出现乱码。

  • 你所说的HTML代码的正统名字叫做HTML 字符实体,英文名字叫做HTML Entities。使用字符实体的话浏览器会自己将代码转换为正确的字符,就少了对编码方式的要求。关于字符实体的内容你可以多看看这些页面:HTML 字符实体 | HTML Entities

总结来说,使用转义的话方便书写和阅读,但是对页面的编码方式有要求。使用HTML字符实体虽然少了编码方式的显示,但是不便于书写和阅读源码。所以我个人的建议是例如"这种稍微大众平常一点的字符还是转义输出比较好,而特殊的,可以独立于页面的特殊字符(如 © ® ™ 等)则使用字符实体的形式表现。字符实体这块虽然使用实体号来写会有更大的兼容性,但是个人倾向于用字符实体的名称,原因还是方便阅读。

转义和转义不一样

感性的来看,但凡在字符串本身当中,出现包裹字符串的分隔符(或其他直接使用有特殊意义的字符),都必须转义。但是不同的语言的转义,虽然目的一致,但要转的东西往往完全不同。

“转义\"好还是用html代码"好”?前者是PHP字符串的转义,后者是HTML语言的转义。使用场合都不同,所以千万不要混为一谈——完全不是等价讨论的概念。

题主问题中,前者使用的PHP字符串转义,就是双引号包裹的字符串当中,允许转义\"="\n=LF(0x0A)等字符。后者涉及的HTML转义就是用HTML实体(HTML Entities)表示字符,比如&=&

什么转义都不能省略

PHP的转义不能省略,不用多说(多数时候,不转义明显解析出错啊!)

但这里的问题是:PHP = Hypertext Preprocessor,PHP的输出就是HTML页面。在PHP中写的字符串常量,经常送给浏览器的HTML解析器。题主问的问题也是如此。

所以必须明确:题主的需求很可能涉及二次转义:先写出合法的PHP字符串常量,然后保证浏览器解析HTML后,输出的仍然是字符串的原样,而不会:

  • 特殊字符失效
  • 破坏HTML DOM结构
  • 被注入代码,产生安全问题

PHP和HTML,两个的转义都是不能省略的。题主问题的结论是:前者只做了PHP的转义,这个放在HTML正文中可以,但放在标签的属性值里不行。后者是做了HTML的转义,而PHP无需转义(视同已转义),OK。但重要的是:要知道“为什么”可以,而不是乱试最后“凑巧”可以。

我赞成的做法

虽然第二种可以,但我认为echo '"';这绝对是一种极其不良的实践。如果评一个“PHP最差编程习惯”,这个绝对有上榜的实力。

因为PHP先构造字符串,再处理成HTML的格式输出,这是一个有先后顺序的需求。如果直接把HTML实体写在PHP的字符串常量里,那就是把两个阶段转义的逻辑混杂在了一起。

在PHP中我比较赞成的方法,是将字符串的内容保持原样,在输出之前用htmlentitieshtmlspecialchars函数包裹之:(注意这两个函数略有区别,网上资料很多)

header("Content-Type: text/html; charset=utf-8");
echo htmlentities("\"M&M\""); # 显示:"M&M" 查看源代码:"M&M"
Copy after login

这样,写源代码的时候只需关心PHP的逻辑。最后由机器保证前台的输出,和字符串本身一致。

你可能总会在网上见到莫名其妙的真的出现一个“&”的情况——这就是不单点转义,而是把转义过程自以为是的到处乱放,最终造成重复做两遍HTML转义的错误。

数据可能到处输出,但数据的原样只有一个。不要把数据的原貌混杂到自己都认不清,这也很重要。

如何对付特殊符号?

对付©®™等特殊符号,以及中文等涉及编码方式的地方,我和 @怡红公子 的看法是不太一致的。我的想法是:

  • 出现任何特殊符号,都真的原样写到字符串中去。中文也一样。
  • 转义仍然使用htmlentities来做。
  • PHP文件尽可能使用UTF-8无BOM格式存储。
  • 如果输出的编码不是UTF-8,则在HTML转义的后边,再加一层iconv转换。

因为我认为转义和编码方式还是一个事情的两个阶段,不宜混在一起谈。这个模型就像俄罗斯套娃一样,安装者一定是先装小的再套大的,而拆解者必然是先拆外边再拆里边。以GBK为例的逻辑就是:

  1. 先有原数据。——©公司"2014"
  2. 做成PHP的字符串常量。——"©公司\"2014\"" 以这个样子写进PHP脚本文件中
  3. 转义HTML实体。——©公司"2014" 此处PHP知道:字符串中有这些字符,这就行了,内部应该是Unicode存的但是我们可以不关心
  4. 转成GBK编码。——©+B9ABCBBE+"2014"中间的一段是4个字节的GBK码原始值
  5. 浏览器解开GBK编码:——©公司"2014"
  6. 再解开HTML实体————©公司"2014"
  7. 原样输出。

去掉编码,无非就是去掉了步骤4和5,和转义一点关系都不涉及到。

輸出HTML的東西都必須轉義(空格-> )。

永遠不要相信一切輸入。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles