I'm using the wysiwyg in the back end and use the following code to limit the string, "...read more" will be displayed at the end of the sentence.
function limit_words($string)
{
$word_limit = '60';
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
<?php echo limit_words($rows['content']); ?>... <a href="index.php">read more</a>
Here is my problem. The "...read more" is showing properly when the string characters are more than 60.
But if the string characters are less than 60, the read more text is showing in new line. I know this is causing by <p>
, which is auto generate from wysiwyg.
Is it possible to solve this issue on this section only?
Solved by using regular expression to remove <p>
tags around elements
$replaced = preg_replace('~</?p[^>]*>~', ' ', $rows['content']);
echo limit_words($replaced); ?>... <a href="index.php">read more</a>