本文介绍了转换&到PHP中的撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据中有很多HTML实体(

既然&< / code>不是HTML 4.01的一部分,在默认情况下,不会转换为'。



在PHP 5.4.0中,来处理不同的语言,每种语言都包含& apos ; 作为一个实体。



这意味着你可以这样做:

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

您将需要 ENT_QUOTES 和双引号)和 ENT_HTML5 (或任何非 ENT_HTML401 的语言标志,因此请根据您的情况选择最适合的语言) 。

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

  echo str_replace('&';','','他们& 。'); 


My data has many HTML entities in it (&bull; ...etc) including &apos;. I just want to convert it to its character equivalent.

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

I tried this:

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

But it returns: They&apos;re here.

Edit:

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

echo html_entity_decode('They&apos;re here.')

also returns: They&apos;re here.

解决方案

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

In PHP 5.4.0, extra flags were introduced to handle different languages, each of which includes &apos; as an entity.

This means you can do something like this:

echo html_entity_decode('They&apos;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).

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

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

这篇关于转换&amp;到PHP中的撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:23