0

So I am tasked with looking through all of the html content areas in my content tree and look for this string:

"<div id="fpCE_version" style="display:none;">8.5.1</div>"

However the version changes with numbers so this 8.5.1 could be 8.5.7 or 8.4.2, so I need a way of looking for that div and replace it with nothing. Here is what I have for only finding the one version so need help with either regex or any other ideas. Thanks

foreach ($item in $itemsToProcess) {
    $items = Get-ItemField $item
    foreach($field in $items){
     $html = $item.Text
     if([String]::IsNullOrEmpty($html)) { continue }
        $newText = $html.Replace('<div id="fpCE_version" style="display:none;">8.5.7</div>', '')
        $item.Text = $newText
    }
}

2 Answers 2

2

You can check with where condition on the id of the HTML tag and replace that with an empty string.

if([String]::IsNullOrEmpty($html)) { continue }
$divTag = $html.getElementsByTagName('div') | Where { $_.id -eq 'fpCE_version' }
if(-not [String]::IsNullOrEmpty($divTag)) 
{
  $newText = $html.Replace($divTag, '')
  $item.Text = $newText
}

If you want to check with the id:

if($html_.id -eq `fpCE_version`)
{
  $item.Text = ''
}

If you want to check with the class name: $html_.className -eq 'your class name'

You can also match with the inner text of the HTML tag:

if ($html.innertext -match "8.") 
{
  $item.Text = ''
}
2
  • I need to keep the remainder of my text that is in these HTML fields. This is just a div at the bottom that I need removed out of the html and leave the other contents alone. Commented Feb 15, 2022 at 23:45
  • Updated the answer, hope it helps. Commented Feb 16, 2022 at 0:01
1

You can run the following script which uses Regex.

If searches for exact <div id="fpCE_version" style="display:none;"> and removes everything from that div, without checking what's inside (assuming there is no other div inside that div).

$regex = '<div id="fpCE_version" style="display:none;">[^<]*</div>'

$fieldsToCheck = @('Text', 'Text 2')

foreach ($item in $itemsToProcess) {
  foreach($field in $fieldsToCheck){
    $html = $item[$field]

    if([String]::IsNullOrEmpty($html)) { 
      continue
    }

    if ($html.Contains('fpCE_version')) {
      $item.Editing.BeginEdit()
      $item[$field] = [System.Text.RegularExpressions.Regex]::Replace($html, $regex, '')
      $item.Editing.EndEdit() | out-null
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.