转换 ' PHP 中的撇号

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

Convert ' to an apostrophe in PHP

phphtmlspecial-charactersdecode

提问by Dave

My data has many HTML entities in it (•...etc) including '. I just want to convert it to its character equivalent.

我的数据中有许多 HTML 实体(•...等),包括'. 我只想将其转换为等效的字符。

I assumed htmlspecialchars_decode() would work, but - no luck. Thoughts?

我认为 htmlspecialchars_decode() 会起作用,但是 - 没有运气。想法?

I tried this:

我试过这个:

echo htmlspecialchars_decode('They're here.');

But it returns: They're here.

但它返回: They're here.

Edit:

编辑:

I've also tried html_entity_decode(), but it doesn't seem to work:

我也试过 html_entity_decode(),但它似乎不起作用:

echo html_entity_decode('They're here.')

also returns: They're here.

还返回: They're here.

回答by cmbuckley

Since 'is not part of HTML 4.01, it's not converted to 'by default.

由于'不是 HTML 4.01 的一部分,因此'默认情况下不会转换为。

In PHP 5.4.0, extra flags were introducedto handle different languages, each of which includes 'as an entity.

在 PHP 5.4.0 中,引入额外的标志来处理不同的语言,每种语言都包含'为一个实体。

This means you can do something like this:

这意味着您可以执行以下操作:

echo html_entity_decode('They're here.', ENT_QUOTES | ENT_HTML5);

You will need both ENT_QUOTES(convert single and double quotes) and ENT_HTML5(or any language flag other than ENT_HTML401, so choose the most appropriate to your situation).

您将同时需要ENT_QUOTES(转换单引号和双引号)和ENT_HTML5(或除 之外的任何语言标志ENT_HTML401,因此请选择最适合您的情况)。

Prior to PHP 5.4.0, you'll need to use str_replace:

在 PHP 5.4.0 之前,您需要使用str_replace

echo str_replace(''', "'", 'They're here.');

回答by eric.itzhak

There is a "right" way, without using str_replace, @cbuckley was right it's because the default for html_entity_decodeis HTML 4.01, but you can set an HTML 5 parameter that will decode it.

有一种“正确”的方式,不使用str_replace, @cbuckley 是正确的,因为默认为html_entity_decodeHTML 4.01,但您可以设置一个 HTML 5 参数来对其进行解码。

Use it like this:

像这样使用它:

html_entity_decode($str,ENT_QUOTES | ENT_HTML5)

回答by J. Bruni

The 'entity and a lot of others are not in the PHP translation table used by html_entity_decodeand htmlspecialchars_decodefunctions, unfortunately.

不幸的'是,实体和许多其他实体不在html_entity_decodehtmlspecialchars_decode函数使用的 PHP 转换表中 。

Check this comment from the PHP manual: http://php.net/manual/en/function.get-html-translation-table.php#73410

从 PHP 手册中查看此评论:http: //php.net/manual/en/function.get-html-translation-table.php#73410

回答by Vikas Shukla

This should work:

这应该有效:

$value = "They're here.";
html_entity_decode(str_replace("'","'",$value));

回答by Matthew Scharley

What you are actually looking for is html_entity_decode().

您实际上正在寻找的是html_entity_decode().

html_entity_decode()translates all entities to characters, while htmlspecialchars_decode()only reverses what htmlspecialchars()will encode.

html_entity_decode()将所有实体转换为字符,而htmlspecialchars_decode()只反转htmlspecialchars()将要编码的内容。

EDIT:Looking at the examples on the page I linked to, I did a bit more investigation and the following seems to not work:

编辑:查看我链接到的页面上的示例,我进行了更多调查,以下内容似乎不起作用:

[matt@scharley ~]$ php
<?php
$tmp = array_flip(get_html_translation_table(HTML_ENTITIES));
var_dump($tmp['&apos;']);
PHP Notice:  Undefined index: &apos; in - on line 3
NULL

This is why it's not working. Why it's not in the lookup table is another question entirely, something I can't answer unfortunately.

这就是它不起作用的原因。为什么它不在查找表中完全是另一个问题,不幸的是我无法回答。

回答by Michael Allen

Have you tried using echo htmlspecialchars('They&apos;re here.')?

你试过使用echo htmlspecialchars('They&apos;re here.')吗?

I think that is what you are looking for.

我认为这就是你要找的。