如何对字符串进行 HTML 编码/转义?有内置的吗? [英] How to HTML encode/escape a string? Is there a built-in?

查看:370
本文介绍了如何对字符串进行 HTML 编码/转义?有内置的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have an untrusted string that I want to show as text in an HTML page. I need to escape the chars '<' and '&' as HTML entities. The less fuss the better.

I'm using UTF8 and don't need other entities for accented letters.

Is there a built-in function in Ruby or Rails, or should I roll my own?

解决方案

The h helper method:

<%=h "<p> will be preserved" %>

这篇关于如何对字符串进行 HTML 编码/转义?有内置的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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