humanemulator.net — справка. Продукт, демо и покупка — на хуманэмулятор.рф | Демо | Купить | API | лог изменений

replace_text

replace_text($path,$text,$new_text); - Заменить текст в WORD документе

Функция на вход принимает параметры:

  • $path – Путь к файлу
  • $text – текст
  • $new_text – новый текст

    После выполнения функции возвращаемое значение будет равно одному из двух :
  • true – успешно
  • false – не удалось

    Пример использования
    <?php
    // Scenario: Replace text in a Word document
    $xhe_host = "localhost:7010";
    // Connect functional objects if not already connected
    if (!isset($path)){
      $path = "../../../Templates/init.php";
      require($path);
    }
    // info
    echo "\n<span >word->" . basename (__FILE__) . "</span>\n";
    
    // Kill any existing Word app processes
    $result = SYSTEM::$word->kill();
    if ($result) {
        echo("Successfully kill Word app process\n");
    } else {
        echo("Failed to kill Word app process\n");
    }
    
    // Define file path and text variables in Linux format
    $filePath = "test/test.docx";
    $searchText = "test";
    $replaceText = "Replaced test";
    
    // Open the file for edit
    SYSTEM::$word->open($filePath, false);
    
    // Example 1: Replace text in the document
    echo("1. Replace text in the document: ");
    $replaceResult = SYSTEM::$word->replace_text($filePath, $searchText, $replaceText);
    if ($replaceResult)
        echo("true\n");
    else
        echo("false\n");
    
    // Save and close the document
    SYSTEM::$word->save($filePath);
    SYSTEM::$word->close($filePath);
    
    echo "\n";
    
    // Quit the application
    WINDOW::$app->quit();
    ?>
    # Additional paths
    import sys
    sys.path.insert(0, '../../../Templates PY/')
    
    xhe_host = "127.0.0.1:7026"
    from xweb_human_emulator import *
    
    # начало
    echo("<hr><font color=blue>sound.xxxxxxxxx</font><hr>")
    
    word.kill();
    path="test\\test.doc";
    
    # 1 
    echo("1. Заменим текст в документе : ");
    echo(word.replace_text(path,"test","Replaced test"));
    
    app.shell_execute("open",path);
    
    # конец
    echo("<hr><br>")
    
    # Quit
    app.quit()
    #region using
    
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Threading;
    
    using XHE;
    using XHE.XHE_DOM;
    using XHE.XHE_System;
    using XHE.XHE_Window;
    using XHE.XHE_Web;
    
    #endregion
    
     class Program:XHEScript
     {
    	  static void Main(string[] args)
    	  {
    			// init XHE
    			server = "localhost:7010";
    			InitXHE();
    
    			// начало
    			echo("<hr><font color=blue>word.add_text</font><hr>");
    
    			word.kill();
    			var path="test\\test.doc";
    
    			// 1 
    			echo("1. Заменим текст в документе : ");
    			echo(word.replace_text(path,"test","Replaced test"));
    
    			app.shell_execute("open",path);
    
    			// конец
    			echo("\n\n");
    
    			app.quit();            
    	  }
    }
    // подключим функциональные объекты, если еще не подключен
    xhe_host="127.0.0.1:7026";
    echo=require("../../../Templates JS/init.js");
    
    // начало
    echo("<hr><font color=blue>sound.beep</font><hr>");
    
    word.kill();
    path="test\\test.doc";
    
    // 1 
    echo("1. Заменим текст в документе : ");
    echo(word.replace_text(path,"test","Replaced test"));
    
    app.shell_execute("open",path);
    
    // конец
    echo("\n");
    
    // Quit
    app.quit();

    =============================================
    word    Объекты    DOM  System  Vision  Web  Window        
    =============================================
    если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум
    .