Friday, February 19, 2010

Add read more link without cutting the word

Many times we have the need to display a short description about web page content. If the string is large and we would like to display only few words and put a link like 'more info', then we have to split the string carefully. We are using substr function to display such content. The problem with substr is , the substr function takes the string up to the limit even the limit is a character in the word. We can avoid such problems by modifying the substr function.

Here the sample code
<?php
$strDes="its a sample text. ";
echo substr($strDes,0,strrpos(substr($strDes,0,64),' '))
?>
... read more
It shows a description without splitting any word.