Page 1 of 1
WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 4:27 am
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?
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 5:11 am
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.
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 5:35 am
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?
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 7:30 am
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.
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 10:26 am
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
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 12:04 pm
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.
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 12:19 pm
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?
Re: WYSIWYG storing - trim by length problem
Posted: Sun Apr 27, 2014 12:25 pm
by Celauran
Just reverse the order of the function calls.
Code: Select all
<?php echo TrimByLength(strip_tags($row_rsNews['newsDesc']), 100, false)); ?>