转换&到PHP中的撇号 [英] Convert ' to an apostrophe in PHP

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

问题描述

我的数据中有很多HTML实体(& bull; ... etc),包括& 。我只是想把它转换成相当的字符。



我假设htmlspecialchars_decode()可以工作,但是 - 没有运气。我想这样做:

  echo htmlspecialchars_decode('They& amp; amp; amp; ;在这里。'); 

但它会返回:他们& re这里。 $ b

编辑:



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

$ p $ echo html_entity_decode('They&'re here。')

也会返回:他们在这里。

解决方案

既然&< / 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之前,您需要使用 str_replace

  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中的撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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