-2

I want to split a paragraph... This is my Paragraph

One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west. A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns, in the precious physical appeal of blown and frosted glass.The horizontal lines which cut across the surface of the lamps give rise to a soft and charming décor and at the same time contribute to filtering the intensity of the light source, diffusing a full, warm and soft light into the room. The blown glass body - with its pleasantly plaster-like appearance - is suspended on a base consisting of three metal feet: a highly distinguishing trait, on which the lamp appears to be floating mid-air. They are perfect for use alone or in a harmonious composition of shapes; on a bedside table, a console and a table or even on the floor: for an area dedicated to relaxation or as meditation lamps, wisely lighting both the space we live in as well as our interior world.

I want it to look like this ...

One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west. A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns, in the precious physical appeal of blown and frosted glass.

means from given paragraph ...I want to remove all words after the 70 word mark.... I used Substr function of php...but its always counts characters....

I need a way to count Words and remove the extra words that come after 70 word mark....

Jamie Eltringham
  • 810
  • 3
  • 16
  • 25
Deepak Rai
  • 164
  • 12

5 Answers5

0

Yes you can do this.

With the str_word_count(); function. More info

Example:

Online demo. (example and demo have 5 words)

$string = 'One, two, three lanterns that are lightweight yet brimming with seductive texture.';

function shorten_string($oldstring, $wordsreturned){
  $string = preg_replace('/(?<=\S,)(?=\S)/', ' ', $oldstring);
  $string = str_replace("\n", " ", $string);
  $array = explode(" ", $string);
  if (count($array)<=$wordsreturned)
  {
    $retval = $string;
  }
  else
  {
    array_splice($array, $wordsreturned);
    $retval = implode(" ", $array)." ...";
  }
  return $retval;
}

if(str_word_count($string, 0) > 5){ // if smaller then 5 words
  echo shorten_string($string, 5).'...'; // we only want 5 words.
} else {
  echo $string; // do nothing, show the string
}

note:
Credits for shorten_string(): https://stackoverflow.com/a/12444960/1501285

Community
  • 1
  • 1
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
0

You have to do some process on that and then you got the ans like following :-

$limit = 5 // Change to 70 for your example
$str = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$arr = explode(" ", str_replace(",", ", ", $str));

for ($index = 0; $index < $limit; $index++) {
    echo $arr[$index]. " ";
}

It may help you.

Harsh Sanghani
  • 1,666
  • 1
  • 14
  • 32
0

Deepak,

I think your 70 words are completing here ....The horizontal lines which cut..

Please, try following code

$str="One, two, three lanterns that are lightweight yet brimming with
seductive texture. Three different yet concurrently complementary
shapes, balancing between the east and the west. A family of lamps
that sheds its relaxing light in the most diverse settings, conveying
the magic of rice paper or mulberry tree bark, the raw materials of
traditional Japanese lanterns, in the precious physical appeal of
blown and frosted glass.The horizontal lines which cut across the
surface of the lamps give rise to a soft and charming décor and at the
same time contribute to filtering the intensity of the light source,
diffusing a full, warm and soft light into the room. The blown glass
body - with its pleasantly plaster-like appearance - is suspended on a
base consisting of three metal feet: a highly distinguishing trait, on
which the lamp appears to be floating mid-air. They are perfect for
use alone or in a harmonious composition of shapes; on a bedside
table, a console and a table or even on the floor: for an area
dedicated to relaxation or as meditation lamps, wisely lighting both
the space we live in as well as our interior world.";

echo get_my_excerpt($str,70);

function get_my_excerpt($str,$length)
{

    $str_segments=explode(" ",$str);

    if(count($str_segments)>=$length)
    {


        $temp_array=array();
        for($i=0;$i<$length;$i++)
        {
            $temp_array[]=$str_segments[$i];
        }

        return implode(" ",$temp_array);
    }
    else
    {
        return $str;
    }

}
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
Asad Khan
  • 116
  • 7
0

Using this regex pattern will give you the first 70 words from the string.

$str = 'One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west. 
A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns, 
in the precious physical appeal of blown and frosted glass. 
The horizontal lines which cut across the surface of the lamps give rise to a soft and charming décor and at the same time contribute to filtering the intensity of the light source, 
diffusing a full, warm and soft light into the room. 
The blown glass body - with its pleasantly plaster-like appearance - is suspended on a base consisting of three metal feet: a highly distinguishing trait, on which the lamp appears to be floating mid-air. 
They are perfect for use alone or in a harmonious composition of shapes; 
on a bedside table, a console and a table or even on the floor: 
for an area dedicated to relaxation or as meditation lamps, wisely lighting both the space we live in as well as our interior world.';

preg_match('#^([^.!?\s]*[\.!?\s]+){0,70}#',$str,$matches);

print $matches[0];

Output

One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west.
A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns,
in the precious physical appeal of blown and frosted glass.
The horizontal lines which

Newlines are because of my formatting,
while the extra The horizontal lines which is because of how counting to 70 words works :)

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
0
    <?php
        $str = "One, two, three lanterns that are lightweight yet brimming with seductive texture. Three different yet concurrently complementary shapes, balancing between the east and the west. A family of lamps that sheds its relaxing light in the most diverse settings, conveying the magic of rice paper or mulberry tree bark, the raw materials of traditional Japanese lanterns, in the precious physical appeal of blown and frosted glass.The horizontal lines which cut across the surface of the lamps give rise to a soft and charming décor and at the same time contribute to filtering the intensity of the light source, diffusing a full, warm and soft light into the room. The blown glass body - with its pleasantly plaster-like appearance - is suspended on a base consisting of three metal feet: a highly distinguishing trait, on which the lamp appears to be floating mid-air. They are perfect for use alone or in a harmonious composition of shapes; on a bedside table, a console and a table or even on the floor: for an area dedicated to relaxation or as meditation lamps, wisely lighting both the space we live in as well as our interior world.";

        $str = str_replace('.', '. ', $str);

        function limit_text($text, $limit)
            {
                if (str_word_count($text, 0) > $limit)
                    {
                        $words = str_word_count($text, 2);
                        $pos = array_keys($words);
                        $text = substr($text, 0, $pos[$limit]);
                    }

                return $text;
            }

        echo limit_text($str, 66);
    ?>
user1544541
  • 193
  • 3