Replacing headings in PHP code

Hello,

I was wondering if anyone knows what to do. I’m just starting out with Powershell. I have files containing PHP code that is saved in .docx format. I need to edit them in bulk within one folder. This works for me. The problem, however, is that I need it to replace my first level heading:

Introduction

with

Introduction

, then

text

with

text

, then also

text

with

text

and so on up to the tenth level heading, so far I’ve only tried up to 4. However, the problem I’m having is that no matter what I try, only the first heading changes either. Or it changes all of them, but to level 4. My current code changes everything to level 4:

$word = New-Object -ComObject Word.Application
$cesta = "C:\Users\475190\Desktop\Cyber"

if (Test-Path $cesta) {
    $subfolders = Get-ChildItem -Path $cesta -Directory

    foreach ($subfolder in $subfolders) {
        $documents = Get-ChildItem -Path $subfolder.FullName -Filter "*.docx"

        foreach ($document in $documents) {
            try {
                $doc = $word.Documents.Open($document.FullName)

                $content = $doc.Content

                $content.Text = $content.Text -replace '<h1>(.*?)</h1>', '<h2>$1</h2>' -replace '<h2>(.*?)</h2>', '<h3>$1</h3>' -replace '<h3>(.*?)</h3>', '<h4>$1</h4>'

                $doc.Close([ref]$true)

            } catch {
                Write-Output "Chyba při zpracování souboru '$($document.FullName)': $($_.Exception.Message)"
            }
        }
    }

    $word.Quit()

} else {
    Write-Host "Vámi zadaná cesta neexistuje, je potřeba správně přepsat umístění složky a poté znovu spustit celý kód"
}

Do you have any tips on what I can do to make it work properly?

No real way for me to test, but maybe this will work:

$Content.Text = (($content.Text.Replace('<h1>(.*?)</h1>', '<h2>$1</h2>')).Replace('<h2>(.*?)</h2>', '<h3>$1</h3>')).Replace('<h3>(.*?)</h3>', '<h4>$1</h4>')

Thank you, I tried it, but unfortunately it didn’t make any change at all.

Hi Jan_sestauber and welcome to the forums!

I would assume that there is text already in this document, no? If so, could you provide us a quick template of what you’re using? This could help us assist.

Yes it is, the texts are longer, but the content of the documents looks something like this:

Hi, I’m trying to remind myself if anyone has any ideas. And just for clarification - I can’t upload the real text here due to copyright issues. I edit research articles for the journal Cyberpsychology.

You can’t post the text you already posted in image form as actual text? You can’t copy/paste from an image and I for one am not willing to type that out to help you. Please make it easier for us to actually help you.

PHP in a Word document sounds like it’s making the problem more difficult than it needs to be. If you just had the PHP in a text file that would make it easier, I think.

$Content = @"
<h1>Introduction</h1>
<h2>Subtitle</h2>
<h3>Content</h3>
<h4>Fluff</h4>
<h5>Footer</h5>
<h6>Really tiny text</h6>
"@

[Regex]::Replace($Content, '(?<=</?h)[1-5](?=>)', {1+$args[0].Value})

Output:

<h2>Introduction</h2>
<h3>Subtitle</h3>
<h4>Content</h4>
<h5>Fluff</h5>
<h6>Footer</h6>
<h6>Really tiny text</h6>

Using the Replace method of the Regex class allows you to use a scriptblock to increment the size by passing in the number it finds. I’ve used non-capturing groups either side of number so only the number gets passed to the scriptblock.

Thank you for the answer. Unfortunately, it converted my php code into the form it would be on the web and was not visible. So I will try what matt-bloomfield advised :cake: and possibly try to paste as text.

Thanks for the tip, I hadn’t thought of that. I’m out of my depth. I’ll give it a try.

Thank you so much, I had to tweak some things in the code, anyway it’s finally working, so I’m happy :slightly_smiling_face:

I had to edit some more things in the code when using .txt, so in the end it looks like this:

$word = New-Object -ComObject Word.Application

$cesta = “C:\Users\475190\Desktop\Cyber”

if (Test-Path $cesta) {

$subfolders = Get-ChildItem -Path $cesta -Directory    



foreach ($subfolder in $subfolders) {    

#pozor v pripade jinych souboru je potreba nahradit priponu .txt na .docx

    $documents = Get-ChildItem -Path $subfolder.FullName -Filter "*.txt"    



    foreach ($document in $documents) {    



        try {   



            $text = Get-Content $document.FullName 

$text = [Regex]::Replace($text -join “rn”, ‘(?<=</?h)[1-5]’, {1+$args[0].Value})

$text | Out-File $document.FullName

        } catch {   



            Write-Output "Chyba při zpracování souboru '$($document.FullName)': $($_.Exception.Message)"   



        }   

    }    

}    



$word.Quit()    

} else {

Write-Host "Vámi zadaná cesta neexistuje, je potřeba správně přepsat umístění složky a poté znovu spustit celý kód"    

}

2 Likes