nectar
nectar

Reputation: 9679

How to capture complete words using substr() in PHP, limit by word?

When I use substr($string,0,100), it gives first 100 characters. Sometimes it left the last word incomplete. That looks odd. Can I do limit by word rather than char?

Upvotes: 9

Views: 27416

Answers (7)

Shuhad zaman
Shuhad zaman

Reputation: 3390

i tried this simple one and it worked for me

<?php echo substr($content, 0, strpos($content, ' ', 200)); ?>

Upvotes: 0

Peter
Peter

Reputation: 2181

This is a solution very similar to that of Scott Evernden, but also works if the character | accidentially occurs in the string:

$result = explode(chr(0), wordwrap($longtext, $len, chr(0)))[0];

Upvotes: 0

Govind Totla
Govind Totla

Reputation: 1178

Try a single line code

$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length));

Upvotes: 1

amateur barista
amateur barista

Reputation: 4520

// Trim very long text to 120 characters. Add an ellipsis if the text is trimmed.
if(strlen($very_long_text) > 120) {
  $matches = array();
  preg_match("/^(.{1,120})[\s]/i", $very_long_text, $matches);
  $trimmed_text = $matches[0]. '...';
}

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838006

If you just count the words the resulting sting could still be very long as a single "word" might have 30 characters or more. I would suggest instead truncating the text to 100 characters, except if this causes a word to be truncated then you should also remove the truncated part of the word. This is covered by this related question:

How to Truncate a string in PHP to the word closest to a certain number of characters?

Using wordwrap

$your_desired_width = 100;
if (strlen($string) > $your_desired_width)
{
    $string = wordwrap($string, 100);
    $i = strpos($string, "\n");
    if ($i) {
        $string = substr($string, 0, $i);
    }
}

This is a modified versions of the answer here. if the input text could be very long you can add this line before the call to wordwrap to avoid wordwrap having to parse the entire text:

$string = substr($string, 0, 101);

Using a regular expression (Source)

$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, 100));

Upvotes: 17

Scott Evernden
Scott Evernden

Reputation: 39940

$a = explode('|', wordwrap($string, 100, '|');
print $a[0];

Upvotes: 3

Matthew
Matthew

Reputation: 48284

I would do something like:

<?php

        function cut_text($text, $len)
        {
                for ($i = 0; $i < 10; ++$i)
                {
                        $c = $text[$len + $i];
                        if ($c == ' ' || $c == "\t" || $c == "\r" || $c == "\n" || $c == '-')
                                break;
                }

                if ($i == 10) $i = 0;

                return rtrim(substr($text, 0, $len + $i));
        }

        echo cut_text("Hello, World!", 3)."\n";
?>

Just start at some point ($len) and move forward a certain number of characters ($i) looking for a breaking character (e.g., whitespace). You could also look backward (-$i) or in both directions, depending on what you are looking for.

Upvotes: 0

Related Questions