为了解决umlauts和特殊字符,我使用了html Object。这是我的函数:
Function ConvertFrom-Html
{
[CmdletBinding(SupportsShouldProcess = $True)]
Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, HelpMessage = "HTML als String")]
[AllowEmptyString()]
[string]$Html
)
try
{
$HtmlObject = New-Object -Com "HTMLFile"
$HtmlObject.IHTMLDocument2_write($Html)
$PlainText = $HtmlObject.documentElement.innerText
}
catch
{
$nl = [System.Environment]::NewLine
$PlainText = $Html -replace '<br>',$nl
$PlainText = $PlainText -replace '<br/>',$nl
$PlainText = $PlainText -replace '<br />',$nl
$PlainText = $PlainText -replace '</p>',$nl
$PlainText = $PlainText -replace ' ',' '
$PlainText = $PlainText -replace 'Ä','Ä'
$PlainText = $PlainText -replace 'ä','ä'
$PlainText = $PlainText -replace 'Ö','Ö'
$PlainText = $PlainText -replace 'ö','ö'
$PlainText = $PlainText -replace 'Ü','Ü'
$PlainText = $PlainText -replace 'ü','ü'
$PlainText = $PlainText -replace 'ß','ß'
$PlainText = $PlainText -replace '&','&'
$PlainText = $PlainText -replace '"','"'
$PlainText = $PlainText -replace ''',"'"
$PlainText = $PlainText -replace '<.*?>',''
$PlainText = $PlainText -replace '>','>'
$PlainText = $PlainText -replace '<','<'
}
return $PlainText
}
示例:
"<p><strong>Nutzen:</strong></p><p>Der Nutzen ist überaus groß.<br />Test ob 3 < als 5 ist & "4" > '2'?" | ConvertFrom-Html
结果:
Nutzen:
Der Nutzen ist überaus groß.
Test ob 3 < als 5 ist & "4" > '2'?