在字符串(PowerShell)中转义HTML特定字符的最佳方法是什么?

时间:2022-09-13 08:00:49

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).

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

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?

那有内置功能吗?

2 个解决方案

#1


35  

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

有一个类将在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;')

#2


3  

Starting with PowerShell 3.0, use [System.Net.WebUtility] for any of the four common operations:

从PowerShell 3.0开始,对四种常见操作中的任何一种使用[System.Net.WebUtility]:

[System.Net.WebUtility]::HtmlEncode('something <somthing else>')
[System.Net.WebUtility]::HtmlDecode('something &lt;somthing else&gt;')
[System.Net.WebUtility]::UrlEncode('something <somthing else>')
[System.Net.WebUtility]::UrlDecode('something+%3Csomthing+else%3E')

[System.Web.HttpUtility]::HtmlEncode is the common approach previous to .NET 4.0 (PowerShell 2.0 or earlier), but would require loading System.Web.dll:

[System.Web.HttpUtility] :: HtmlEncode是.NET 4.0(PowerShell 2.0或更早版本)之前的常用方法,但需要加载System.Web.dll:

Add-Type -AssemblyName System.Web

Starting with .NET 4.0 (PowerShell 3.0) [System.Web.HttpUtility]::HtmlEnocde internally calls [System.Net.WebUtility]::HtmlEncode, therefore it makes sense to leave out the middle man (System.Web.dll).

从.NET 4.0开始(PowerShell 3.0)[System.Web.HttpUtility] :: HtmlEnocde在内部调用[System.Net.WebUtility] :: HtmlEncode,因此省略中间人(System.Web.dll)是有意义的。

#1


35  

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

有一个类将在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;')

#2


3  

Starting with PowerShell 3.0, use [System.Net.WebUtility] for any of the four common operations:

从PowerShell 3.0开始,对四种常见操作中的任何一种使用[System.Net.WebUtility]:

[System.Net.WebUtility]::HtmlEncode('something <somthing else>')
[System.Net.WebUtility]::HtmlDecode('something &lt;somthing else&gt;')
[System.Net.WebUtility]::UrlEncode('something <somthing else>')
[System.Net.WebUtility]::UrlDecode('something+%3Csomthing+else%3E')

[System.Web.HttpUtility]::HtmlEncode is the common approach previous to .NET 4.0 (PowerShell 2.0 or earlier), but would require loading System.Web.dll:

[System.Web.HttpUtility] :: HtmlEncode是.NET 4.0(PowerShell 2.0或更早版本)之前的常用方法,但需要加载System.Web.dll:

Add-Type -AssemblyName System.Web

Starting with .NET 4.0 (PowerShell 3.0) [System.Web.HttpUtility]::HtmlEnocde internally calls [System.Net.WebUtility]::HtmlEncode, therefore it makes sense to leave out the middle man (System.Web.dll).

从.NET 4.0开始(PowerShell 3.0)[System.Web.HttpUtility] :: HtmlEnocde在内部调用[System.Net.WebUtility] :: HtmlEncode,因此省略中间人(System.Web.dll)是有意义的。