编码问题,覆盖& to & for html 使用 php

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

Encoding issue, coverting & to & for html using php

phpcharacter-encodinghtml-entities

提问by JMC

I have a url in html:

我在 html 中有一个网址:

<a href="index.php?q=event&amp;id=56&amp;date=128">

I need to turn it into a string exactly as:

我需要把它完全变成一个字符串:

<a href="index.php?q=event&id=56&date=128">

I know how to do this with preg_replace etc, but is there a function in php that deals directly with encoding that I can use for other encoding issues such as &nsbp (or whatever it is, etc)? Ideally I would send my string into the function and it would output '&' instead of &amp. Is there a universal function for converting &TEXT; into an actual character?

我知道如何使用 preg_replace 等来做到这一点,但是 php 中是否有一个函数可以直接处理编码,我可以将其用于其他编码问题,例如 &nsbp(或其他任何编码等)?理想情况下,我会将我的字符串发送到函数中,它会输出 '&' 而不是 &。是否有转换 &TEXT; 的通用函数?变成一个真实的角色?

Edit: sorry, posted this before I finished typing the question. QUESTION is now complete.

编辑:抱歉,在我输入问题之前发布了这个。问题现已完成。

回答by Sergey Eremin

use html_entity_decode():

使用html_entity_decode()

$newUrl = html_entity_decode('<a href="index.php?q=event&amp;id=56&amp;date=128">');
echo $newUrl; // prints <a href="index.php?q=event&id=56&date=128">

回答by mhitza

Use htmlspecialchars_decode. Example straight from the PHP documentation page:

使用htmlspecialchars_decode。直接来自 PHP 文档页面的示例:

$str = '<p>this -&gt; &quot;</p>';
echo htmlspecialchars_decode($str); // <p>this -> "</p>

回答by Rupert Madden-Abbott

There is no built in PHP function that will take an entity such as &amp;and turn it into a double &. Just in case there is any confusion, the html entity for & is actually &amp;, not amp;, so running any built in parser on your example will return the following:

没有内置的 PHP 函数可以将实体(例如)&amp;转换为双 &。以防万一有任何混淆, & 的 html 实体实际上是&amp;,不是amp;,因此在您的示例上运行任何内置解析器将返回以下内容:

<a href="index.php?q=event&id=56&date=128">

and not

并不是

<a href="index.php?q=event&&id=56&&date=128">

In order to get the double & version, you will need to use a regular expression.

为了获得双 & 版本,您需要使用正则表达式。

Alternatively, if you in fact want the single & version, you have two possibilities.

或者,如果您实际上想要单个 & 版本,则有两种可能性。

  1. If you just wish to convert &amp; &quot; &#039; &lt; &gt;then you should use htmlspecialchars_decode. This would be sufficient for the example you give.
  2. If you wish to convert any string of the format &TEXT; then you should use html-entity-decode.
  1. 如果你只是想转换,&amp; &quot; &#039; &lt; &gt;那么你应该使用htmlspecialchars_decode。这对于您给出的示例就足够了。
  2. 如果您希望转换任何格式为 &TEXT; 的字符串。那么你应该使用html-entity-decode

I suspect that htmlspecialchars_decode will be faster than html_entity_decode so if it covers all the entities you wish to convert, you should use that.

我怀疑 htmlspecialchars_decode 会比 html_entity_decode 快,所以如果它涵盖了你想要转换的所有实体,你应该使用它。

回答by dev31

after string go through TinyMCE only this code help me

在字符串通过 TinyMCE 后,只有这段代码对我有帮助

$string = iconv('UTF-8','cp1251',$string);
$string = str_replace(chr(160), chr(32), $string);
$string = iconv('cp1251','UTF-8',$string);