Search code examples
phpsubstr

How to skip HTML tags when using substr to grab the first x number of characters


In some blogfiles, i use a "readmore" button after 500 characters. To grab the first 500 characters from a string (which is the blogmessage), i use substr like below:

$blog_message_reduced = substr($blog_message, 0, 500);

the string $blog_message looks like this:

lorem ipsum is simply dummy text of the printing and typesetting industry <img src="data/uploads/image.jpg" class="img-responsive" style="width: 100%" /> and some more text behind the image.... 

Sometimes it happens, when I'm writing a blog article, that the 500 chars limit is exactly inside the img tag.

$blog_message_reduced and the HTML output is then something like:

lorem ipsum is simply dummy text of the <img src="data/uplo

In the above example, 500 has reached with the o of the word uploads.

So I am looking for a way to ignore the img tags in the substr when cropping with 500.(never cut an img tag when 500 has reached; in that case, cut immediately after the img tag).

How can I achieve this?


Solution

  • Use PHP's strip_tags() for this before cropping.

    <?php
    
    $str = 'lorem ipsum is simply dummy text of the printing and typesetting industry <img src="data/uploads/image.jpg" class="img-responsive" style="width: 100%" /> and some more text behind the image.... ';
    $pre = strip_tags($str);
    $crop = substr($pre, 0, 100);
    echo $crop;
    
    // Output:
    // lorem ipsum is simply dummy text of the printing and typesetting industry and some more text behind
    

    or the same with some more advanced usage

    <?php
    
    $str = 'lorem ipsum is simply dummy text of the printing and typesetting industry <img src="data/uploads/image.jpg" class="img-responsive" style="width: 100%" /> and some more text behind the image.... ';
    echo crop($str, 100, '... (read more)', true, true);
    
    function crop($content, $maxCharacters, $append = '...', $respectWordBoundaries = false, $stripTags = false)
    {
        if ($stripTags) {
            $content = strip_tags($content);
        }
    
        if ($maxCharacters) {
            if (mb_strlen($content, 'utf-8') > abs($maxCharacters)) {
                $truncatePosition = false;
                if ($maxCharacters < 0) {
                    $content = mb_substr($content, $maxCharacters, null, 'utf-8');
                    if ($respectWordBoundaries) {
                        $truncatePosition = strpos($content, ' ');
                    }
                    $content = $truncatePosition ? $append . substr($content, $truncatePosition) : $append . $content;
                } else {
                    $content = mb_substr($content, 0, $maxCharacters, 'utf-8');
                    if ($respectWordBoundaries) {
                        $truncatePosition = strrpos($content, ' ');
                    }
                    $content = $truncatePosition ? substr($content, 0, $truncatePosition) . $append : $content . $append;
                }
            }
        }
        return $content;
    }