Can PHP functions be used to ensure that HTML tags are properly closed when cutting off content?

When cutting off content in PHP, it's important to ensure that HTML tags are properly closed to avoid breaking the structure of the page. One way to achieve this is by using PHP functions like `strip_tags()` in combination with `substr()` to truncate the content while preserving the HTML structure.

function truncate_content($content, $length) {
    $truncated_content = substr($content, 0, $length);
    $truncated_content = strip_tags($truncated_content);
    return $truncated_content;
}

// Example usage
$content = "<p>This is some <strong>sample</strong> content.</p>";
$truncated_content = truncate_content($content, 20);
echo $truncated_content;