php 替换 ' 以及带有相应字符的类似 html 代码?

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

Replace ' and similar html codes with their correspondent character?

php

提问by Jonas Kaufmann

so I have a string that goes like this:

所以我有一个像这样的字符串:

$string = "This is a test string. It has characters like these: '";

Is there a php function that transforms these to their correspondent character, in my example the desired output would be:

是否有一个 php 函数可以将它们转换为对应的字符,在我的示例中,所需的输出是:

print $string
// OUTPUT: This is a test string. It has characters like these: '

回答by Yaron U.

yes there is: htmlspecialchars_decode($string, ENT_QUOTES);

就在这里: htmlspecialchars_decode($string, ENT_QUOTES);

not sure about the specific 'char, as far as I know htmlspecialchars(with ENT_QUOTESflag) convert an apostrophe (') to '(with a leading zero)

不确定具体的'字符,据我所知htmlspecialchars(带ENT_QUOTES标志)将撇号(')转换为'(带前导零)

so the exact behavior on 'worth checking

所以'值得检查 的确切行为

EDIT: I made the test and it does work :)

编辑:我做了测试,它确实有效:)

回答by edwardmp

You can use html_entity_decode()

您可以使用html_entity_decode()

It's like reverse htmlentities. If you use quotes in your input string you have to set the second parameter of html_entity_decode()to ENT_QUOTES

这就像反向htmlentities。如果在输入字符串中使用引号,则必须将第二个参数设置html_entity_decode()ENT_QUOTES

See it in action: http://sandbox.onlinephpfunctions.com/code/7f4649eb47a8e639c514787a100b63bbad4bc8c6

看到它在行动:http: //sandbox.onlinephpfunctions.com/code/7f4649eb47a8e639c514787a100b63bbad4bc8c6