WYSIWYG storing - trim by length problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

WYSIWYG storing - trim by length problem

Post by jonnyfortis »

I am using a WYSIWYG NicEdit text editor. its storing all the formatted text in my database (phpMyAdmin). It diplays in output fine unless i was to trim the text to only show part text.
an example of stored text is:
[text]<br><div><span style="font-family: Arial, Helvetica, sans; font-size: 11px; line-height: 14px; text-align: justify;">Contrary to popular belief, Lorem Ipsum is not simply random text.</span></div>[/text]

the trim im using is

Code: Select all

function TrimByLength($str, $len, $word) {
  $end = "";
  if (strlen($str) > $len) $end = "...";
  $str = substr($str, 0, $len);
  if ($word) $str = substr($str,0,strrpos($str," ")+1);
  return $str.$end;
}
the output is

Code: Select all

<?php echo TrimByLength($row_rsNews['newsDesc'], 100, false); ?>
if i use this nothing is displayed on the page. if i display with no trim it all shows fine

any ideas?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: WYSIWYG storing - trim by length problem

Post by requinix »

You can't just cut an HTML string at some position: it might cut in the middle of a tag and screw up in any number of possible ways.

I take it this is for a preview of the content? Do a strip_tags() on it and cut the result - it won't have all the HTML markup that the original has, but the alternative is crazy complicated.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: WYSIWYG storing - trim by length problem

Post by jonnyfortis »

requinix wrote:You can't just cut an HTML string at some position: it might cut in the middle of a tag and screw up in any number of possible ways.

I take it this is for a preview of the content? Do a strip_tags() on it and cut the result - it won't have all the HTML markup that the original has, but the alternative is crazy complicated.
yes its a preview of content. where does the strip_tags() go?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: WYSIWYG storing - trim by length problem

Post by Celauran »

Right before the trim. strip_tags to remove the HTML, leaving you with just plain text, then you're safe to truncate that wherever you choose.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: WYSIWYG storing - trim by length problem

Post by jonnyfortis »

Celauran wrote:Right before the trim. strip_tags to remove the HTML, leaving you with just plain text, then you're safe to truncate that wherever you choose.
brilliant
thanks

Code: Select all

<?php echo strip_tags(TrimByLength($row_rsNews['newsDesc'], 100, false)); ?>
worked fine
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: WYSIWYG storing - trim by length problem

Post by Celauran »

That's backwards. You're trimming the length then stripping the HTML tags. You want to remove the tags first so you don't encounter the problems requinix described.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: WYSIWYG storing - trim by length problem

Post by jonnyfortis »

Celauran wrote:That's backwards. You're trimming the length then stripping the HTML tags. You want to remove the tags first so you don't encounter the problems requinix described.
oh i see, how do i do that?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: WYSIWYG storing - trim by length problem

Post by Celauran »

Just reverse the order of the function calls.

Code: Select all

<?php echo TrimByLength(strip_tags($row_rsNews['newsDesc']), 100, false)); ?>
Post Reply