I'm having a little problems trying to print HTML in one page. I have a substr to show the first X chars of the text, and a button to show more. My problem is that if I use html_decode or html_entity_decode to print as HTML (and not only as html), it breaks the page and show all the text instead of the first X chars and the "Show more" button.
PHP:
$item->description = html_decode($item->description);
if( strlen( $item->description ) > 70 ) {
$item->description = substr($item->description, 0, 50 ) .
'<span class="more-link">... <a href="#"><i class="fa fa-caret-down"></i> ' . t( 'theme_more', 'More' ) . '</a></span>' .
'<span class="hidden-part">' . substr( $item->description, 50 ) . '</span>' .
'<span class="less-link"><a href="#"><i class="fa fa-caret-up"></i> ' . t( 'theme_less', 'Less' ) . '</a></span>';
}
The JS Code:
$( '.description .more-link a, .description .less-link a' ).on( 'click', function(e) {
e.preventDefault();
var desc = $(this).parents( '.description' );
var part = desc.find( '.hidden-part' ),
more = desc.find( '.more-link' ),
less = desc.find( '.less-link' );
if( part.is( ':visible' ) ) {
part.hide();
more.show();
less.hide();
} else {
part.show();
more.hide();
less.show();
}
});
If I change the html_decode to strip_tags just to debug, it works as supposed, but prints the HTML chars as plain text (show the first X chars and then in the Show More button, it shows the rest of the text). With html decode, basically it shows all the text and create the button in the middle of the text.
My problem is not about the HTML tags being cutted with substr.
Can anyone help me please? Thanks