Forum


Replies: 5   Views: 2278
Searchandreplace by bland space does not work
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 legalvision  · 19-02-2019 - 16:22

Hi there,

I have a searchAndReplace by blank spaces as follow:

$newDocx->searchAndReplace(
    'myFile.docx',
    'myFile2.docx',
    ' ' . $placeholder . ' ',
    '       '
);

I am pretty sure this use to work, but does not anymore. I have checked in the searchAndReplace function and the $node->nodeValue output seems to be fine. My guess is that this has something do to with the underlying XML and how Microsoft deals with it. What made me think that is that if I open the resulting Word document, there is no space. But if I preview it (space bar in MacOS), I can see the spaces.

Any idea how to replace a placeholder by blank space ? My goal here is to remove a placeholder by searchAndReplace, and not have 2 spaces remaining, but just one.

Posted by legalvision  · 20-02-2019 - 09:33

Hi there, 

Thanks for the suggestion, but from what I understand docxpath removeWordContent is useful when you want to remove some text/paragraph where you can find the placeholder inside, not the exact placeholder. For example, when looking at the OOXML of my document, the placeholder might be inside a <w:t> node alongside some other text. How do I target ONLY the placeholder?

Posted by admin  · 20-02-2019 - 10:29

Hello,

You can get the node element using the getDOCXPathQueryInfo method (https://www.phpdocx.com/api-documentation/docx-path/get-docx-path-query-info) querying by the placeholder. This method returns an array with three keys: elements, length and query. You can get the XML value of the DOM node through the elements content, do the needed changes to the XML and replace the source node by the one as raw WordML content (https://www.phpdocx.com/api-documentation/word-content/add-raw-wordml-Word-document).

Regards.

Posted by legalvision  · 21-02-2019 - 13:12

Unfortunately that does not solve my problem. Looking at the XML below:

<w:p w14:paraId="3C0351BD" w14:textId="5E16754D" w:rsidR="00A642C9" w:rsidRDefault="00953216"><w:r><w:t>For any additional signage ~IL_testInline~</w:t></w:r><w:r w:rsidR="003478B0"><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>required by</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve"> the Tenant.</w:t></w:r></w:p>

My goal would be to remove ~IL_testInline~, leaving only 1 space between "signage" and "required". But in this case, removing my placeholder in the XML string would not capture the space that is isolated in the following part:

<w:t xml:space="preserve"> </w:t>

What do you suggest?

Posted by admin  · 21-02-2019 - 15:13

Hello,

You can use the previous method to accomplish what you need, but can't do it only in a single step, as there's no method to accomplish it directly. You need to:

1. Load the XML returned by the getDOCXPathQueryInfo method, this is a DOM object so you can manipulate it.

2. Remove the placeholder and get if there's a blank space before (or after if needed) the placeholder in the same tag or in the previous sibling (or next sibling if removing the space after the placeholder) and remove it as well.

3. Replace the source element by the new one using DOCXPath.

If you open a support ticket attaching a DOCX sample (https://www.phpdocx.com/support), the dev team will generate a custom code using it.

Some users with the same case of use, instead of adding blank spaces around the placeholder:

text $PLACEHOLDER$ text

they remove them in the template:

text$PLACEHOLDER$text

So the placeholder can be removed without needing to clear other blank spaces or replaced with a new text string with blank spaces at the beginning/end.

Regards.