substr() Not Returning Correct Number of Characters

I’m trying to get the page content and shorten it to 320 characters. I’m using the following:

$post_content = get_post_field(‘post_content’, $post_id);
$post_content_stripped = strip_tags($post_content);
echo substr($post_content_stripped, 0, 320);

This returns strings of different lengths depending on the post, but it is always close to 320, just not always exactly 320. Sometimes it’s 319 or 318.

Why does it not always return a 320 character string?

Read more here:: substr() Not Returning Correct Number of Characters

Leave a Reply

Your email address will not be published. Required fields are marked *