问题描述
|
本质上,我正在获取包含诸如
&
和>
之类的实体的源代码,我想分别用&和>代替它们。简单的字符串替换将完全穷举,因为源代码中可能显示数百种可能的实体。是否有内置的或标准的方法来执行此操作,因此我不必键入一百行字符串替换?
谢谢! :)
解决方法
也许您正在寻找WebUtility.HtmlDecode方法
本文提供了一个使用Powershell进行调用的示例
HTMLDecode Sample - Using PowerShell
<#
.SYNOPSIS
This script encodes and decodes an HTML String
.DESCRIPTION
This script used
.NOTES
File Name : Show-HtmlCoding.ps1
Author : Thomas Lee - [email protected]
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted tot:
http://msdn.microsoft.com/en-us/library/ee388364.aspx
.EXAMPLE
PSH [C:\\foo]: .\\Show-HtmlCoding.ps1
Original String: <this is a string123> & so is this one??
Encoded String : <this is a string123> & so is this one??
Decoded String : <this is a string123> & so is this one??
Original string = Decoded string?: True
#>
# Create string to encode/decode
$Str = \"<this is a string123> & so is this one??\"
# Encode String
$Encstr = [System.Net.WebUtility]::HtmlEncode($str)
# Decode String
$Decstr = [System.Net.WebUtility]::HtmlDecode($EncStr)
# Display strings
\"Original String: {0}\" -f $Str
\"Encoded String : {0}\" -f $Encstr
\"Decoded String : {0}\" -f $Decstr
$eq = ($str -eq $Decstr)
\"Original string = Decoded string?: {0}\" -f $eq
, 我将使用sed控制台命令并为此编写一个小脚本。尝试用Google sed 1行。我敢打赌你喜欢它。