转义字符串中特定于HTML的字符(PowerShell)的最佳方法是什么? [英] What is the best way to escape HTML-specific characters in a string (PowerShell)?

查看:92
本文介绍了转义字符串中特定于HTML的字符(PowerShell)的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PowerShell脚本生成一些简单的HTML,并且我想要转义结果HTML中使用的字符串(因为它们可以包含一些HTML特定的符号)。



<例如:

  $ a =something< somthing else>; 

应转换为以下内容:

 something& lt; something else& gt; 

是否有内置函数?

解决方案

有一个类可以在System.Web中做到这一点。

  Add-Type -AssemblyName System.Web 
[System.Web.HttpUtility] :: HtmlEncode('something< somthing else>')

甚至可以用其他方式:

  [System.Web.HttpUtility] :: HtmlDecode('something& lt; something else& gt;')


I'm generating some simple HTML with PowerShell script, and I would like to escape strings used in result HTML (since they can contain some HTML-specific symbols).

For example:

$a = "something <somthing else>";

should be converted to the following:

"something &lt;something else&gt;"

Is there any built-in function for that?

解决方案

There's a class that will do this in System.Web.

Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlEncode('something <somthing else>')

You can even go the other way:

[System.Web.HttpUtility]::HtmlDecode('something &lt;something else&gt;')

这篇关于转义字符串中特定于HTML的字符(PowerShell)的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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