[英]Replace characters in a string with their HTML coding
I need to replace characters in a string with their HTML coding. 我需要用其HTML编码替换字符串中的字符。
Ex. 例如 The "quick" brown fox, jumps over the lazy (dog). 敏捷的棕色狐狸跳过了懒狗)。
I need to replace the quotations with the & quot; 我需要用&替换引号。 and replace the brakets with & #40; 并用( and & #41; 和)
I have tried str_replace, but I can only get 1 character to be replaced. 我已经尝试过str_replace,但是我只能替换1个字符。 Is there a way to replace multiple characters using str_replace? 有没有办法使用str_replace替换多个字符? Or is there a better way to do this? 还是有更好的方法来做到这一点?
Thanks! 谢谢!
PHP has a number of functions to deal with this sort of thing: PHP具有许多函数来处理这种事情:
Firstly, htmlentities()
and htmlspecialchars()
. 首先, htmlentities()
和htmlspecialchars()
。
But as you already found out, they won't deal with (
and )
characters, because these are not characters that ever need to be rendered as entities in HTML. 但是,正如您已经发现的那样,它们不会处理(
和)
字符,因为这些字符不是必须以HTML实体形式呈现的字符。 I guess the question is why you want to convert these specific characters to entities? 我猜问题是为什么您要将这些特定字符转换为实体? I can't really see a good reason for doing it. 我真的看不出这样做的充分理由。
If you really do need to do it, str_replace()
will do multiple string replacements, using arrays in both the search and replace paramters: 如果确实需要这样做, str_replace()
将使用搜索和替换参数中的数组进行多个字符串替换:
$output = str_replace(array('(',')'), array('(',')'), $input);
You can also use the strtr()
function in a similar way: 您也可以类似的方式使用strtr()
函数:
$conversions = array('('=>'(', ')'=>')');
$output = strtr($conversions, $input);
Either of these would do the trick for you. 这些都可以为您解决问题。 Again, I don't know why you'd want to though, because there's nothing special about (
and )
brackets in this context. 同样,我也不知道为什么要这么做,因为在这种情况下, (
和)
括号没有什么特别的。
While you're looking into the above, you might also want to look up get_html_translation_table()
, which returns an array of entity conversions as used in htmlentities()
or htmlspecialchars()
, in a format suitable for use with strtr()
. 在研究以上内容时,您可能还需要查找get_html_translation_table()
,它以适合与strtr()
一起使用的格式返回htmlentities()
或htmlspecialchars()
使用的实体转换数组。 You could load that array and add the extra characters to it before running the conversion; 您可以在运行转换之前加载该数组并向其中添加额外的字符; this would allow you to convert all normal entity characters as well as the same time. 这将允许您同时转换所有普通实体字符。
I would point out that if you serve your page with the UTF8 character set, you won't need to convert any characters to entities (except for the HTML reserved characters <
, >
and &
). 我要指出的是,如果为您的页面提供UTF8字符集,则无需将任何字符转换为实体(HTML保留字符<
, >
和&
除外)。 This may be an alternative solution for you. 这可能是您的替代解决方案。
You also asked in a separate comment about converting line feeds. 您还在另一条评论中询问有关转换换行的信息。 These can be converted with PHP's nl2br()
function, but could also be done using str_replace()
or strtr()
, so could be added to a conversion array with everything else. 这些可以使用PHP的nl2br()
函数进行转换,但也可以使用str_replace()
或strtr()
进行转换,因此可以与其他所有内容一起添加到转换数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.