目前我有这段代码:
http://pastebin.com/L4GvLkB1该正则表达式当前查找所有不包含alt标签的图像。
我想更进一步,添加一个alt属性,其中包含内容,作为src属性中图像的名称。
一如既往,我们非常感谢所有的建议或帮助。
如果你想知道自动化功能是做什么的,在这里:
function automate($school, $query, $replace) {
$processFiles = Get-ChildItem -Exclude *.bak -Include "*.html", "*.HTML", "*.htm", "*.HTM" -Recurse -Path $school
foreach ($file in $processFiles) {
#$text = Get-Content $file
$text = Get-Content $file | Out-String
$text = $text -replace $query, $replace
$text | Out-File $file -Force -Encoding utf8
}
}
试试这样:
Get-ChildItem -Exclude *.bak -Include *.html, *.htm -Recurse -Path $school | % {
$html = New-Object -COM HTMLFile
$html.Write([IO.File]::ReadAllText($_.FullName))
$html.getElementsByTagName('img') | % {
$_.alt = Split-Path -Leaf $_.src
}
[IO.File]::WriteAllText($html.documentElement.outerHTML)
}