Jump to content

truncate removing html tags


bibsch
 Share

Recommended Posts

According to the docs it doesn't keep <p> tags:

https://processwire.com/blog/posts/processwire-3.0.101-core-updates/

Quote

keepFormatTags (BOOLEAN)
When true, this keeps all HTML text formatting tags in the returned string, stripping only the block level ones. The tags it leaves are: abbr, acronym, b, big, cite, code, em, i, kbd, q, samp, small, span, strong, sub, sup, time, and var.

What you want is keepTags so you can specify the tags to keep - I think the only catch is that you'll need to specify a lot more than just 'p' like in my example.

image.png.7df77c2e2b8ec38d7f7588db2711f436.png

Perhaps keepFormatTags should include <p> - maybe something to mention to Ryan on Github?

Link to comment
Share on other sites

http://84.151.221.133/q2jump/blog/

 

<?php
include("./head.inc");
foreach($page->children() as $child) {
  echo "<h3>$child->title</h3>";
  echo $page->createdUser->name;
  echo $sanitizer->truncate($child->body, $maxLength = 200, $options = [
    'keepTags' => array('p'),
  ]);
  echo "<a href='$child->url'>Read more...</a><hr>";
}
include("./foot.inc");

whats happens... only my last child body is wrapped in <p> 

Link to comment
Share on other sites

2 hours ago, Zeka said:

I this test case 'truncate' method also removes paragraph tags. I have tried different combinations of 'keepTags' and 'keepFormatTags' without luck.

I tested it too, and the problem occurs when the paragraph is longer than the truncate length, causing there to be no closing </p> tag present in the truncated string.

There is an option for the fixUnclosedTags() method used by truncate, to close instead of remove unclosed tags...

* Fix/close unclosed tags:
* ------------------------
* When the remove option is false, it will attempt to close unclosed tags rather than 
* remove them. It doesn't know exactly where they should be closed, so it appends the 
* close tags to the end of the string. 

...but the truncate method calls fixUnclosedTags() with remove = true and doesn't provide a way to do otherwise. I guess you could use WireTextTools::fixUnclosedTags() before calling $sanitizer->truncate but it would be nice to have the option built into the truncate method. Maybe an oversight by Ryan?
Edit: request opened here: https://github.com/processwire/processwire-requests/issues/253

 

@bibsch, because your truncate length is short and will only contain a single paragraph you can achieve your objective by adding <p> tags around your truncated text:

echo '<p>' . $sanitizer->truncate($child->body, 200) . '</p>';

 

  • Like 5
Link to comment
Share on other sites

  • 4 years later...
On 1/6/2019 at 10:46 PM, Robin S said:

I tested it too, and the problem occurs when the paragraph is longer than the truncate length, causing there to be no closing </p> tag present in the truncated string.

There is an option for the fixUnclosedTags() method used by truncate, to close instead of remove unclosed tags...

* Fix/close unclosed tags:
* ------------------------
* When the remove option is false, it will attempt to close unclosed tags rather than 
* remove them. It doesn't know exactly where they should be closed, so it appends the 
* close tags to the end of the string. 

...but the truncate method calls fixUnclosedTags() with remove = true and doesn't provide a way to do otherwise. I guess you could use WireTextTools::fixUnclosedTags() before calling $sanitizer->truncate but it would be nice to have the option built into the truncate method. Maybe an oversight by Ryan?
Edit: request opened here: https://github.com/processwire/processwire-requests/issues/253

 

@bibsch, because your truncate length is short and will only contain a single paragraph you can achieve your objective by adding <p> tags around your truncated text:

echo '<p>' . $sanitizer->truncate($child->body, 200) . '</p>';

 

I am stumbling over and over again over this issue and I really think this should be fixed, or at least the 'remove' option of the fixUnclosedTags() Function should be adjustable in the $sanitizer->truncate() method. @ryan

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...