Php 使用按字数读取更多内容拆分字符串

Php 使用按字数读取更多内容拆分字符串,php,string,split,Php,String,Split,我需要一个包含2个div的拆分字符串,第一个div包含20个单词,最后一个div包含其余的单词,以使read more与javascript链接 目前我只有字符数限制 如何进行分词 if ( $term && ! empty( $term->description ) ) { $first = substr($term->description, 0, 400); $rest = substr($term->des

我需要一个包含2个div的拆分字符串,第一个div包含20个单词,最后一个div包含其余的单词,以使read more与javascript链接

目前我只有字符数限制

如何进行分词

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>';
        }

此代码实现以下功能:

<?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>";
使用SplitStringTopParts函数。在您的情况下,您应将其称为: SplitStringTopParts$term->description,$first,$rest,20

在$first之后,$rest将保留您的结果

找到的解决方案:

<?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);

也许可以做作业,将每个单词拆分为限制,而不是第一个限制,其余不拆分。已经测试过了。对于一个数组,取前20个元素,返回一个字符串,然后取其可能的重复项
<?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);