Search code examples
phpwordpresssubstr

Cut off text after a certain amount of characters via substr fail


I have a wordpress site for news, where i have to cut the text after certain amount of characters. It all works fine via substr(), but when there are less than x characters it goes crazy and generate a nonsence in the link of the next <li>.

This is the code:

echo '<li>', get_the_date( 'd.m.Y', $recent["ID"] ), '&nbsp;&nbsp;<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
$post_id = $recent["ID"];
$post_content = get_post($post_id);
$content = $post_content->post_content;
echo mb_substr(do_shortcode( $content ), 0, 200 , "utf-8");
echo "...<br> <br>";
echo "</div>";

Solution

  • In the $content is there any HTML? If so and that gets chopped off, it will make the next 'li' be invalid HTML, perhaps... Just found this, might be of help

    Remove all html tags from php string