Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<em> <strong> generates a line break on ::addHtml() #1410

Open
1 of 3 tasks
tom-lp-at opened this issue Jun 11, 2018 · 10 comments
Open
1 of 3 tasks

<em> <strong> generates a line break on ::addHtml() #1410

tom-lp-at opened this issue Jun 11, 2018 · 10 comments

Comments

@tom-lp-at
Copy link

tom-lp-at commented Jun 11, 2018

This is:

Expected Behavior

Dies ist ein Test

DIES IST EIN TEST
• wrewrewr
• cxvycxvy
ertfetertert

The HTML-Table is read from a MySQL-Field and stored from a ckeditor

Current Behavior

Dies ist ein Test

DIES
IST

EIN
TEST
• wrewrewr
• cxvycxvy
ertfetertert
What is the current behavior?
After the Bold and Italic it puts a line-break into

Failure Information

Please help provide information about the failure.
The resulting XML from the docx:

<w:t xml:space="preserve">Dies ist ein Test</w:t></w:r></w:p><w:p/><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">DIES </w:t></w:r></w:p><w:p><w:pPr/><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t xml:space="preserve">IST</w:t></w:r></w:p><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve"> </w:t></w:r></w:p><w:p><w:pPr/><w:r><w:rPr><w:i/><w:iCs/></w:rPr><w:t xml:space="preserve">EIN</w:t></w:r></w:p><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve"> TEST</w:t></w:r></w:p><w:p><w:pPr><w:pPr/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="9"/></w:numPr></w:pPr><w:r><w:rPr/><w:t xml:space="preserve">wrewrewr</w:t></w:r></w:p><w:p><w:pPr><w:pPr/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="9"/></w:numPr></w:pPr><w:r><w:rPr/><w:t xml:space="preserve">cxvycxvy</w:t></w:r></w:p><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">ertfetertert</w:t></w:r></w:p>

How to Reproduce

The source of the Table:

Dies ist ein Test<br />
DIES <strong>IST</strong> <em>EIN</em> TEST
<ul>
<li>wrewrewr</li>
<li>cxvycxvy</li>
</ul>
ertfetertert
<?php
               $phpWord = new \PhpOffice\PhpWord\PhpWord();
 		\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
 		
 		$phpWord->getSettings()->setUpdateFields(true);
 		
		$phpWord->setDefaultFontName('CorporateS');
 		$phpWord->addParagraphStyle('pJustify', array('align' => 'center', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0));
 		$phpWord->addFontStyle('footerStyle', array('size'=>8));
 		
 		// New portrait section
		$section = $phpWord->addSection();
                $section->addTitle('Zweckbestimmung des Verzeichnisses', 1);
 		\PhpOffice\PhpWord\Shared\Html::addHtml($section, $anwendung['zweck'],false,false);
                $phpWord->save($path_pdf1."/".$file_docx,"Word2007");

Context

  • Fedora 27 Workstation - latest updates
  • PHP version: 7.1.18
  • PHPWord version: dev-master

I use phpword installed over composer into a codeigniter-project. I try 0.14 / 0.15.x-dev / dev-master / dev-develop. I double checked with the pulled changes from ticket #846 and they are all in place. I try to put it between a <p> and <div>. The only solution for now is to remove the tags after reading from the database.

@tom-lp-at
Copy link
Author

i try to put it between a <p> + </p> and <div> + </div> - no success

@troosan
Copy link
Contributor

troosan commented Jul 10, 2018

only way I managed to have it working is

<p>Dies ist ein Test<br/>DIES <strong>IST</strong> <em>EIN</em> TEST</p>
<ul>
    <li>wrewrewr</li>
    <li>cxvycxvy</li>
</ul>

@jr-ewing
Copy link

jr-ewing commented Aug 11, 2019

is there an other solution or an Bugfix for this ?
I did not want to change every Text and put it in

.

@schreyers
Copy link

schreyers commented Mar 25, 2020

I was having this issue as well, and I have found this solution to work currently.

function clean_comment_export($subject) { $subject = preg_replace('#\R+#', "\n\n", strip_tags(preg_replace("/[\r\n]+/", "\n", $subject), '<b><i><strike>')); $subject = nl2br('<p>'. $subject . '</p>'); return $subject; }

This works well enough for my use, so hope it helps here :)

@dorigoor
Copy link

dorigoor commented Apr 28, 2021

I was having this issue as well, and I have found this solution to work currently.
`function clean_comment_export($subject) {
$subject = preg_replace('#\R+#', "\n\n", strip_tags(preg_replace("/[\r\n]+/", "\n", $subject), ''));

$subject = nl2br('<p>' . $subject . '</p>');
return $subject;

}`
This works well enough for my use, so hope it helps here :)

that removed the tags that cause the line breaks.
but I want to keep the visual effect of the tags.
for example, I want the text that is in the "strong" tag to appear bold and of course without the line break.

so as of April 2021 - is there a solution for this?

by the way, the unwanted line breaks happens on every HTML tag (using the addHtml() method).
example:
<p>pretext <strong>text</strong> posttext</p>
or:
<p>pretext <inc>text</inc> posttext</p>
or:
<p>pretext <p style="display: inline">text</p> posttext</p>
or:
<p>pretext <foo>text</foo> posttext</p>
the result of all of the above is a line break after "pretext" and after "text", for example:
pretext
text
posttext

@schreyers
Copy link

schreyers commented Apr 28, 2021

@dorigoor the solution I use only targets double
tags (i think) and only leaves in the <b><i><strike>

I guess you could add to the checks and look for patterns that emerge.

Also noticed that the code block wasn't displaying properly, so fixed that here

@vikashrajgupta
Copy link

I'm facing a similar problem here, I have a string that contains the <b> tag, and whenever I'm using the below method to show content as HTML it's breaking the line.
I'm using this currently \PhpOffice\PhpWord\Shared\Html::addHtml($cell, $string,false,false);
Can you please help me out here?

@schreyers
Copy link

@vikashrajgupta Its been so long since I solved this, but I run my text through this function, and its seems to do the trick. The HTML that gets input is horrific, so I have to do a lot of steps to clean it.

`function clean_comment_export($subject, $front_addition_text = '') {

// Clean escaped quotes
$subject = str_replace('\"', '"', $subject);

// Clean style tags
$subject = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $subject);

// Remove multiple br tags
$subject = preg_replace('#(<br */?>\s*)+#i', '<br />', $subject);

// Try and catch all the weird spaces...
$subject = str_replace(array("<br />", "<br/>"), "</p><p>", '<p>'.$subject.'</p>');

// Sort the spacing
$subject = preg_replace('#\R+#', "\r\n", preg_replace("/[\r\n]+/", "\r\n", $subject));

// Remove Tabs 
$subject = trim(preg_replace('/\t+/', '', $subject));

// Remove multiple backslashes
$subject = preg_replace('~\\+~', '', $subject);

// Remove multiple single quote
$subject = preg_replace("~'+~", "'", $subject);

// Remove \' 
$subject = str_replace("\'", "'", $subject);

// Turn closing and opening p tags into breaks
$subject = str_replace("</p><p>", "###", $subject);

// Remove the double ######
$subject = str_replace("######", "", $subject);

// ...and ### ### as these happen apparently.
$subject = str_replace("### ###", "###", $subject);

// Remove nbsp
$subject = str_replace("&nbsp;", ' ', $subject);

// Remove double spaces 
$subject = preg_replace('!\s+!', ' ', $subject);

// Strip all tags, except <b><i><strike>
$subject = strip_tags($subject, '<b><i><strike>');

// Remove any ### at the start
$subject = rtrim(ltrim($subject, "#"), "#");

// Add in the <p> tags in the text.
$subject = str_replace("###", "</p><br/><p>", $subject);

// Sort the breaks before and after formatting tags
$subject = '<p>' .nl2br($front_addition_text . $subject). '</p>';

return $subject;

}`

@elishaukpong
Copy link

@schreyers your fix just saved me after 3 hours of figuring out what exactly is wrong. i replaced <br> with <br/> and I got the new line I needed.

Thank you

@Hanzerik
Copy link

Had the same problem, @schreyers 's function indeed does the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants