Replies: 10   Views: 5815
Please fix 'parselinebreaks' option
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 fantasma  · 06-11-2014 - 15:46

The parseLineBreak option does not work. The code used in CreateDocxFromTemplate function replaceListVariable line: 493 is wrong: $value = str_replace(array('\n\r', '\r\n', '\n', '\r'), '__LINEBREAK__', $value); should be: $value = str_replace(array("\n\r", "\r\n", "\n", "\r"), '__LINEBREAK__', $value); there are other code functions with about the same issue. Please fix it and provide a patch or a new version Kind regards

Posted by admin  · 06-11-2014 - 16:33

Hello, We have done some tests and everything run fine. For example, Templates/replaceVariableByText/sample_1.php runs perfectly in Linux and Windows using PHP 5.2, PHP 5.3, PHP 5.4 and PHP 5.6. Did you check you don't have some config doesn't allow to use the single quote? Because that function just replace a character in the strings. Regards.

Posted by fantasma  · 07-11-2014 - 09:09

Sorry, but it does not work ! I'm running Ubuntu 14.04, PHP 5.5 and phpdocx 4.1. and Ubuntu 12.04, PHP 5.3, phpdocx 4.1. On both platforms it does not work! I fixed it for me, replacing "\r\n" with '__LINEBREAK__' before replacing the variable. $break = '__LINEBREAK__'; $multiline = str_replace(array("\n\r", "\r\n", "\n", "\r"), $break, $text); You need to use double-quotes (") to make your code work. Please have a look at: Kind regards

Posted by admin  · 07-11-2014 - 09:57

Hello, We have tested in on Debian, Ubuntu, CentOS, RedHat, Fedora, Windows and Mac OS X and it works perfectly. You need to use doble quotes if you need to interpret the content, but phpdocx doesn't need that because it replaces a string that doesn't need to interpreted but replaced. If your server needs it, you can extends the current class and change it to don't touch the code of the library. But of course we're going to prepare a VM with your exact configuration to check it directly. Regards.

Posted by fantasma  · 07-11-2014 - 12:55

I'm really sorry, but in my case the str_replace with single quotes just does not find the breaks (Tested with aptana, xdebug). By the way, i'm using Drupal 7 and im generating DOCX from the Drupal MySQL Database. Database encoding is UTF8. For the moment i can go on with my workaround and i'm not going to change the library :-) Thanks for your help and kind regards,

Posted by sigura  · 06-01-2016 - 16:35

I confirm that this is still an issue. Replacing the single quotes by double quotes works and fixes this issue.

We had this issue on our development environment as well as on our production environments.

I have replaced this in the CreatDocxFromTemplate file, and hoping this won't break in a future update.

Ps: using PHP 5.6.x on Ubuntu/CentOS

Posted by admin  · 06-01-2016 - 17:59

Edited by admin · 06-01-2016 - 18:19


All the tests done about this issue passed without any change, using all OS described in the previous posts, so maybe it's a special PHP config.

Please run this sample using the PHP CLI mode withouth changing anything in the library:


If the error persists, we'd like to test it directly. Please send to contact[at] an email, we'd need access through SSH to run the library through CLI mode.


Posted by admin  · 07-01-2016 - 08:35


We have tested it again using fresh installations of Ubuntu and CenOS and it works perfectly. We'd need to check it directly on your server, please write to contact[at]

The most common mistake when working with parseLineBreaks is that you need to set line breaks as '\n' not "\n".


Posted by quasydoc  · 22-01-2016 - 14:25

We also ran into this issue.

To reproduce it, try saving the contents of a textarea form field containing multiple lines of text to the database. If you then retrieve the field, and feed it into the replaceVariableByText() function with the parseLineBreaks option 'true', line breaks are not detected by phpdocx.

What "str_replace(array('\n\r', '\r\n', '\n', '\r'), '__LINEBREAK__', $value);" in the phpdocx source code does is look for the character '\', followed by 'r', followed by character '\', followed by 'n', or in hex notation: 5C 72 5C 6E, instead of 0D 0A.

It should work in the same fashion as the nl2br() php function.

Posted by admin  · 22-01-2016 - 15:10


The current version of phpdocx needs a literal '\n' to use the line breaks option, not a carriage return.

We have added a new task to add carriage return support in this option. At the moment the only solution is to use '\n' (or any '\n\r', '\r\n', '\n', '\r') to use the line breaks option. If you're using a text area please replace the carriage return by a literal '\n' before adding it.


Posted by admin  · 26-01-2016 - 12:16


We have uploaded a patch to add carriage returns support in both classic and namespace packages.

Until the release of a next version of phpdocx, all users with an active License Update Service may download them on the private area.