I'm PHP newbie and can't find a solution for this:
My website prints correctly a product description, which is sometimes very long. Therefore I would like to limit the text to e.g. 100 words and then display "read more..." which would initiate a java script to display the rest of the text.
Here's what I currently have and what works fine so far without text limitation:
<?php echo nl2br($property->description(null, null, true)); ?>
And here's what I was advised to do:
<?php $long_text = ($property->description(null, null, true)); // STRING!
$word_limit = 250; //your word limit, Int
$output = "";
$parts = explode( " ", $long_text );
foreach( $parts as $index=>$word ){
$output .= "$word ";
if( intval( $index ) >= intval( $word_limit ) ){
$output .= "READ MORE LINK";
break;
}
}
echo $output;
?>
As a result the script limits the text indeed to 250 words, but
- all line breaks and blank lines are ignored
- "READ MORE..." is just plain text - not a link. I would need the code to call a javascript, to print the rest of the text on the same page.
Can someone please help to finalize this last part ?