Forum


Replies: 4   Views: 6870
Replace template variable with text containing new line
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 medscope  · 29-11-2012 - 02:17

[b]I have these text in the template[/b]

Thank you for referring $Name$ for a Home Medication Review. $P_GName$ was interviewed on $Date_of_Review$.$extraText$All medications were reviewed in light of the information you provided in the referral and that obtained during the interview.

[b]where $extraText$ could be empty or text with multiple lines.[/b]

[b]if it's empty the output will be[/b]
Thank you for referring $Name$ for a Home Medication Review. $P_GName$ was interviewed on $Date_of_Review$. All medications were reviewed in light of the information you provided in the referral and that obtained during the interview.

[b]if it's not empty, the output will be[/b]
Thank you for referring $Name$ for a Home Medication Review. $P_GName$ was interviewed on $Date_of_Review$.
/*this is new line*/
Warning, it's extra text for following reasons:
- Reason 1
/*this is new line*/
All medications were reviewed in light of the information you provided in the referral and that obtained during the interview.

[b]but the addTemplateVarible can't recognise \n and the replaceTemplateVariableByHTML can't recognise <br> at the start and the end of $extraText$ the output will become[/b]
Thank you for referring $Name$ for a Home Medication Review. $P_GName$ was interviewed on $Date_of_Review$. Warning, it's extra text for following reasons:
- Reason 1 All medications were reviewed in light of the information you provided in the referral and that obtained during the interview.

[b]so How can I replace that template variable with text into a seperated paragraph[/b]

Posted by lorepra  · 11-04-2013 - 12:13

Hi, I have resolved this way:

$testo='First line
Second line';
$testo=lf2lf($testo);
$aTesto=explode('\\n',$testo);
foreach ($aTesto as $testo) {
$docx->addText($testo);
}

function lf2lf($text) {
$return=str_replace("\\n",'\\n',$text);
$return=str_replace("\\r\\n",'\\n',$return);
return $return;
}

Hope this help,

Posted by medscope  · 11-04-2013 - 12:13

Thanks for you reply.

what i did was replacing a template variable, the addText can't work for me.

Posted by lorepra  · 11-04-2013 - 12:13

If I have it right, you can use replaceTemplateVariableByHTML, converting line feed to < /br>,

[code]
$text='first line
second line';

$text=str_replace("\n",'\n',$text);

$text=str_replace('\n','<br />',$text);

$parHTML=array('isFile' => false, 'parseDivsAsPs' => true,'downloadImages' => false);

$docx->replaceTemplateVariableByHTML('templateVariable','inline',$text,$parHTML);
[/code]

Regards,