I
I
Ivan_Ivanv2020-11-03 14:29:55
PHP
Ivan_Ivanv, 2020-11-03 14:29:55

How to change text in many *.docx files based on filename?

- There are 1000 pcs. MS WORD files with .docx

5fa13bcd81e32218879697.jpeg

extension - The name of each file is numbers in order from 1568 to 2568 .

- In each file, you need to change the number 15 to the file name without the file extension. - There is a PHP

script that does this for .txt and .html files and other files that are edited by notepad. But this script does not change the text inside .docx files and "cripples" these files. What is wrong in the script?


<?php

     
for ($i=1568; $i  < 1593; $i++) {                    //  диапазон  имен файлов которые читать нужно
    $file = file_get_contents("C:/1/{$i}.docx");     //  C:/1/ в папку 1 нужно скинуть файлы в которые нужно поменять текст на имя файлов
    $nextfile = str_replace('15', $i, $file); 
    file_put_contents("C:/1/{$i}.docx", $nextfile); //сохраняем
}

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lillipup, 2020-11-03
@Ivan_Ivanv

So you can't edit docx, docx is essentially xml in the archive.
If I understand the task correctly, then here you need to use this https://www.php.net/manual/ru/function.rename.php
(In general, you need to use rename to rename files)
And to read and edit the docx file itself, you can use this https://github.com/PHPOffice/PHPWord

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question