Forum


Replies: 2   Views: 590
Problems with character & in bulkprocessing
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by michael.stubenvoll  · 17-03-2021 - 09:32

Hello,
i just encounterd a problem in the bulkProcessing method replaceText:
When i execute the following function call, only int the last generated document $PLACEHOLDER$ will be replaced with the correct "test test" value. All the other $PLACEHOLDER$ will be just removed but not replaced with the containing value. This happens when the option "parseLineBreaks" is true or false.

$bulk->replaceText([["PLACEHOLDER" => "test & test"], ["PLACEHOLDER" => "test&"], ["PLACEHOLDER" => "&"], ["PLACEHOLDER" => "test test"]]);

I´m am using phpdocx version 11.
Greetings
Michael

Posted by admin  · 17-03-2021 - 10:00

Hello,

Please send the DOCX template you are using to contact[at]phpdocx.com so we can check it.

Also please note that BulkProcessing (https://www.phpdocx.com/documentation/introduction/bulk-processing-documents-PHP) uses arrays in arrays to generate the documents, for example (included sample BulkProcessing/replaceText/sample_2.php):

$variables = 
array(
    // first DOCX
    array(
        'date' => date('Y/m/d'),
        'name' => 'John',
        'address' => 'Acme Street 1',
        'url' => 'https://www.phpdocx.com',
        'signature' => 'phpdocx',
        'footer' => 'phpdocx\nfooter',
    ),
    // second DOCX
    array(
        'date' => date('Y/m/d'),
        'name' => 'Mary',
        'address' => 'Acme Street 2',
        'url' => 'https://www.phpdocx.com',
        'signature' => 'phpdocx',
        'footer' => 'phpdocx\nfooter',
    ),
    // third DOCX
    array(
        'date' => date('Y/m/d'),
        'name' => 'Tom',
        'address' => 'Acme Street 3',
        'url' => 'https://www.phpdocx.com',
        'signature' => 'phpdocx',
        'footer' => 'phpdocx\nfooter',
    ),
);
$options = array(
        'parseLineBreaks' => true,
);

$bulk->replaceText($variables, $options);

The first subarray is only for the first DOCX to be generated, the second subarray for the second DOCX to be generated and so on. Maybe you setting the placeholders for multiple DOCX when you need to set them for a single DOCX?

BulkProcessing feature (https://www.phpdocx.com/documentation/introduction/bulk-processing-documents-PHP) allows generating multiple DOCX outputs based on the same template. But if you need to generate a single DOCX output from a template, the best solution is using template methods (https://www.phpdocx.com/documentation/practical/working-with-templates).

Regards.

Posted by admin  · 17-03-2021 - 15:13

Hello,

Thanks for sending the sample that illustrates the issue. The current version of BulkProcessing doesn't apply htmlspecialchars to the new contents automatically (other methods of phpdocx apply this function automatically), so the strings that contains & don't appear, you can solve it applying htmlspecialchars to the contents to be added:

$bulk->replaceText(
  [
    ["PLACEHOLDER" => htmlspecialchars("test & test")],
    ["PLACEHOLDER" => htmlspecialchars("test&")],
    ["PLACEHOLDER" => htmlspecialchars("&")],
    ["PLACEHOLDER" => "test test"]
  ]
);
$documents = $bulk->getDocuments();

We have opened a task to apply htmlspecialchars automatically in the next version of BulkProcessing class.

Regards.