0

I need a split string with 2 divs, by the first div with 20 words and last div with rest of words to make read more link with javascript.

At the moment I have only character count limit.

How can I do word splitting?

if ( $term && ! empty( $term->description ) ) {
            $first = substr($term->description, 0, 400);
            $rest = substr($term->description, 400);
            echo '<div class="term-description"><div class="first-letter">'.$first.'</div><div class="last-letter">'.$rest.'</div></div>';
        }
Foxsk8
  • 308
  • 1
  • 9
  • 21
  • Maybe http://php.net/manual/en/function.wordwrap.php can do the job – Med Apr 26 '18 at 15:11
  • WordWrap split every words limit, not first limit and rest without split. Already tested. – Foxsk8 Apr 26 '18 at 15:12
  • [explode](http://php.net/manual/en/function.explode.php) to an array, take the first 20 elements, [implode](http://php.net/manual/en/function.implode.php) back to a string, then take the rest – Patrick Q Apr 26 '18 at 15:14
  • https://stackoverflow.com/questions/4258557/limit-text-length-in-php-and-provide-read-more-link – Sourav Sachdeva Apr 26 '18 at 15:16
  • Possible duplicate of [Display post excerpts, limited by word count](https://stackoverflow.com/questions/8990007/display-post-excerpts-limited-by-word-count) – Patrick Q Apr 26 '18 at 15:16

2 Answers2

1

This code does the trick:

<?php

function SplitStringToParts($sourceInput, &$first, &$rest, $countWordsInFirst = 20)
{
    $arr_exploded = explode(" ", $sourceInput);

    $arr_part1 = array_slice($arr_exploded, 0, $countWordsInFirst);
    $arr_part2 = array_slice($arr_exploded, $countWordsInFirst);

    $first = implode(" ",$arr_part1);
    $rest = implode(" ",$arr_part2);    
}

$str = "str1 str2 str3 str4 str5 str6 str7 str8 str9 str10 str11 str12 str13 str14 str15 str16 str17 str18 str19 str20 str21 str22 str23 str24";

SplitStringToParts($str,$first,$rest,20);

echo $first."<br>";
echo $rest."<br>";

Output is:

str1 str2 str3 str4 str5 str6 str7 str8 str9 str10 str11 str12 str13 str14 str15 str16 str17 str18 str19 str20
str21 str22 str23 str24

Use SplitStringToParts function. In your case you should call it as: SplitStringToParts($term->description, $first, $rest, 20);

After it $first, $rest will keep your result

Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15
1

Found solution:

<?php
// sentence teaser
// this function will cut the string by how many words you want
function word_teaser($string, $count){
  $original_string = $string;
  $words = explode(' ', $original_string);

  if (count($words) > $count){
   $words = array_slice($words, 0, $count);
   $string = implode(' ', $words);
  }

  return $string;
}

// sentence reveal teaser
// this function will get the remaining words
function word_teaser_end($string, $count){
 $words = explode(' ', $string);
 $words = array_slice($words, $count);
 $string = implode(' ', $words);
  return $string;
}
?>

$string = "We are BrightCherry web design, and we're going to show you how to write a function to crop a string  by a certain amount of words."

//this will echo the first 10 words of the string
echo word_teaser($string, 10);

$string = "We are BrightCherry web design, and we're going to show you how to write a function to crop a string by a certain amount of words."

//this will echo the words after the first 10 words
echo word_teaser_end($string, 10);
Foxsk8
  • 308
  • 1
  • 9
  • 21