更好/更清洁的方法根据单词数分割字符串?

时间:2014-06-13 07:34:15

标签: php arrays string slice

我需要一种方法来计算PHP中单词(不是字符)的数量,并在HTML中启动<SPAN>标记以包围指定后的剩余单词数。

我查看了诸如 wordwrap str_word_count 之类的功能,但那些似乎没有帮助。我继续修改了这里的代码:http://php.timesoft.cc/manual/en/function.str-word-count.php#55818

一切似乎都很好用,但是我想发布这里的代码来自2005年,也许有一种更现代/更有效的方法来处理我想要实现的目标?

<?php
$string = "One two three four five six seven eight nine ten.";

// the first number words to extract
$n = 3;

// extract the words
$words = explode(" ", $string);

// chop the words array down to the first n elements
$first = array_slice($words, 0, $n);

// chop the words array down to the retmaining elements
$last = array_slice($words, $n);

// glue the 3 elements back into a spaced sentence
$firstString = implode(" ", $first);

// glue the remaining elements back into a spaced sentence
$lastString = implode(" ", $last);


// display it
echo $firstString;
echo '<span style="font-weight:bold;"> '.$lastString.'</span>';
?>

4 个答案:

答案 0 :(得分:3)

您可以使用preg_split()代替正则表达式。这是this answer的修改版本,其改进的正则表达式使用正面的后观:

function get_snippet($str, $wordCount) {
    $arr = preg_split(
        '/(?<=\w)\b/', 
        $str, 
        $wordCount*2+1, 
        PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
    );

    $first = implode('', array_slice($arr, 0, $wordCount));
    $last = implode('', array_slice($arr, $wordCount));

    return $first.'<span style="font-weight:bold;">'.$last.'</span>';
}

用法:

$string = "One two three four five six seven eight nine ten.";
echo get_snippet($string, 3);

输出:

  

一二三四五六七八九十。

Demo

答案 1 :(得分:1)

让我们更简单。试试这个

<?php

    $string = "One two three four five six seven eight nine ten.";

    // the first number words to extract
    $n = 2;

    // extract the words
    $words = explode(" ", $string);

    for($i=0; $i<=($n-1); $i++) {
      $firstString[] = $words[$i];  // This will return one, two
    }

    for($i =$n; $i<count($words); $i++) {
      $firstString[] = $words[$i];  // This will return three four five six seven eight nine ten
    }

    print_r($firstString);
    print_r($firstString);
?>

<强> Demo here

答案 2 :(得分:0)

我从这里借了代码:

https://stackoverflow.com/a/18589825/1578471

/**
 * Find the position of the Xth occurrence of a substring in a string
 * @param $haystack
 * @param $needle
 * @param $number integer > 0
 * @return int
 */
function strposX($haystack, $needle, $number){
    if($number == '1'){
        return strpos($haystack, $needle);
    }elseif($number > '1'){
        return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle));
    }else{
        return error_log('Error: Value for parameter $number is out of range');
    }
}

$string = "One two three four five six seven eight nine ten.";

$afterThreeWords = strposX($string, " ", 3);

echo substr($string, 0, $afterThreeWords); // first three words

答案 3 :(得分:0)

这对我来说很好,这是另一种方法,你可以检查这个效率? 我不知道哪个更快。我猜你的是更长的字符串更快

$string = "This is some reasonably lengthed string";
$n = 3;
$pos = 0
for( $i = 0; $i< $n; $i++ ){
    $pos = strpos($string, ' ', $pos + 1);
    if( !$pos ){
        break;
    }
}
if( $pos ){
    $firstString = substr($string, 0, $pos);
    $lastString = substr($string, $pos + 1);
}else{
    $firstString = $string;
    $lastString = null;
}