逆 htmlentities / html_entity_decode
基本上我想把一个字符串变成这样:
;
blabla;
改为:
<code>
blabla
我该怎么做?
The use case (bc some people were curious):
像 this 这样的页面,其中包含允许的 HTML 标签和示例列表。例如, 是一个允许的标签,这就是示例:
<code><?php echo "Hello World!"; ?></code>
我想要一个反向函数,因为有很多这样的标签和示例,我将它们全部存储到一个我迭代的数组中在一个循环中,而不是单独处理每个循环......
Basically I want to turn a string like this:
<code> <div> blabla </div> </code>
into this:
<code> <div> blabla </div> </code>
How can I do it?
The use case (bc some people were curious):
A page like this with a list of allowed HTML tags and examples. For example, <code>
is a allowed tag, and this would be the sample:
<code><?php echo "Hello World!"; ?></code>
I wanted a reverse function because there are many such tags with samples that I store them all into a array which I iterate in one loop, instead of handling each one individually...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的版本使用正则表达式:
它尝试匹配每个标签和文本节点,然后分别应用htmlentities和html_entity_decode。
My version using regular expressions:
It tries to match every tag and textnode and then apply htmlentities and html_entity_decode respectively.
没有现有的功能,但看看这个。
到目前为止,我只在您的示例上测试了它,但此功能应该适用于所有 htmlentities
更新:
感谢 @Mike 花时间用其他字符串测试我的函数。我已将正则表达式从
/(\&(.+)\;)/
更新为/(\&([^\&\;]+)\;) /
应该可以解决他提出的问题。我还添加了
{2,6}
来限制每次匹配的长度,以减少误报的可能性。将正则表达式从
/(\&([^\&\;]+){2,6}\;)/
更改为/(&([^&] ;;]+){2,6};)/
删除不必要的扩展。哇哦,脑电波!将正则表达式从
/(&([^&]+){2,6};)/
更改为/(&(#?\w){2,6 };)/
进一步降低误报概率!There isn't an existing function, but have a look at this.
So far I've only tested it on your example, but this function should work on all htmlentities
Update:
Thanks to @Mike for taking the time to test my function with other strings. I've updated my regex from
/(\&(.+)\;)/
to/(\&([^\&\;]+)\;)/
which should take care of the issue he raised.I've also added
{2,6}
to limit the length of each match to reduce the possibility of false positives.Changed regex from
/(\&([^\&\;]+){2,6}\;)/
to/(&([^&;]+){2,6};)/
to remove unnecessary excaping.Whooa, brainwave! Changed the regex from
/(&([^&;]+){2,6};)/
to/(&(#?\w){2,6};)/
to reduce probability of false positives even further!单独更换对您来说还不够。无论是正则表达式还是简单的字符串替换,因为如果替换 < > 符号,那么 <和>符号或反之亦然,您最终将得到一种编码/解码(所有 < 和 > 或所有 < 和 > 符号)。
因此,如果您想执行此操作,则必须解析出一组(我选择用占位符替换)进行替换,然后将它们放回并进行另一次替换。
Replacing alone will not be good enough for you. Whether it be regular expressions or simple string replacing, because if you replace the < > signs then the < and > signs or vice versa you will end up with one encoding/decoding (all < and > or all < and > signs).
So if you want to do this, you will have to parse out one set (I chose to replace with a place holder) do a replace then put them back in and do another replace.
我想我有一个小解决方案,为什么不将 html 标签分解成一个数组,然后根据需要进行比较和更改?
修改....没有错误。
I think i have a small sollution, why not break html tags into an array and then compare and change if needed?
Modified .... with no errors.
因此,尽管这里的其他人推荐了正则表达式,这可能是绝对正确的方法......我想发布此内容,因为它足以满足您提出的问题。
假设您始终使用 html'esque 代码:
给我以下输出:
相当肯定这不会支持再次向后退.. 正如发布的其他解决方案一样,在以下意义上:
So, although other people on here have recommended regular expressions, which may be the absolute right way to go ... I wanted to post this, as it is sufficient for the question you asked.
Assuming that you are always using html'esque code:
Gives me the following output:
Fairly certain that this wouldn't support going backwards again .. as other solutions posted, would, in the sense of:
我建议使用正则表达式,例如 preg_replace():
http://www.php.net/manual/en/function.preg-replace.php
http://www.webcheatsheet.com/php/regular_expressions.php
http://davebrooks.wordpress.com/2009/04/22/php-preg_replace-some-useful-regular-expressions/
I'd recommend using a regular expression, e.g. preg_replace():
http://www.php.net/manual/en/function.preg-replace.php
http://www.webcheatsheet.com/php/regular_expressions.php
http://davebrooks.wordpress.com/2009/04/22/php-preg_replace-some-useful-regular-expressions/
编辑:看来我还没有完全回答你的问题。没有内置的 PHP 函数可以执行您想要的操作,但您可以使用正则表达式甚至简单表达式进行查找和替换: str_replace, preg_replace
Edit: It appears that I haven't fully answered your question. There is no built-in PHP function to do what you want, but you can do find and replace with regular expressions or even simple expressions: str_replace, preg_replace