【问题标题】:PHP Word Count FunctionPHP 字数统计功能
【发布时间】:2015-07-16 20:47:25
【问题描述】:

我正在尝试编写我的第一个自定义函数。我知道还有其他功能可以做同样的事情,但这个是我的。我写了函数,但我不理解 char_list 因为它与函数有关,并且无法找出 php.ini 中 str_word_count function 的第三个参数。我想我需要以某种格式来维护句点、逗号、分号、冒号等。请注意,在整个函数中都维护了双引号和单引号。从字符串中剥离的是底部符号。

$text = "Lorem ipsum' dolor sit amet, consectetur; adipiscing elit. Mauris in diam vitae ex imperdiet fermentum vitae ac orci. In malesuada."

function textTrim($text, $count){ 
  $originalTxtArry = str_word_count($text, 1);

  $shortenTxtArray = str_word_count(substr(text, 0,$count), 1);
  foreach ($shortenTxtArray as $i=>$val) {
    if ($originalTxtArry[$i] != $val) {
      unset($shortenTxtArray[$i]);
    }
  }
  $shortenTxt = implode(" ", $shortenTxtArray)."...";
  return $shortenTxt;
} 

输出 Lorem ipsum' dolor sit amet consectetur adipiscing elit Mauris in diam...

注意amet 后面的“,”不见了。

忽略末尾的句号字符串,我在返回之前将它们连接到末尾

感谢您的所有帮助。

戴夫

【问题讨论】:

  • 抱歉,你的函数应该做什么?你向它发送什么输入,预期输出是什么,你实际得到什么?
  • 你有什么问题@Dave?
  • 第三个参数是单词中可以包含的任何其他有效字符。看看他们提供的例子,看起来很简单。他们在单词 fri3nd 中有一个“3”,在第三个参数中没有“3”,这个词被认为是“fri”。
  • 这个函数的目的是什么,你是想限制字数吗?
  • 如果你不想担心丢失标点符号,那么你可以简单地在一个空格上爆炸然后加入。

标签: php arrays string word-count


【解决方案1】:

来自 PHP 手册中关于第三个参数 charlist 的介绍:

将被视为“单词”的附加字符列表

这些是通常的 a-z 之外的任何字符,应该作为单词的一部分包含在内,并且不会导致单词中断。

如果您查看链接到的 PHP 手册中的示例 1,它将显示一个示例,其中当 charlist 参数中包含 3 时,单词 'fri3nd' 仅被归类为 1 个单词。

【讨论】:

  • 说明如何制作,以便在整个函数中保留逗号和句点
  • 根据函数定义和文档,您可以将第三个参数设置为“,”。 (quote-comma-period-quote)
【解决方案2】:

更新了基于空间爆炸的功能

function textTrim($str, $limit){ 
    /** remove consecutive spaces and replace with one **/
    $str = preg_replace('/\s+/', ' ', $str);

    /** explode on a space **/
    $words = explode(' ', $str);

    /** check to see if there are more words than the limit **/
    if (sizeOf($words) > $limit) {
        /** more words, then only return on the limit and add 3 dots **/
        $shortenTxt = implode(' ', array_slice($words, 0, $limit)) . '...';
    } else {
        /** less than the limit, just return the whole thing back **/
        $shortenTxt = implode(' ', $words);
    }
    return $shortenTxt;
}

【讨论】:

  • 嘿伙计,这都是错误。但它给了我正确的想法。我修好了一切。将在一分钟内发布答案
  • 很抱歉,我什至没有测试它。更新函数以解决语法错误。
  • 我排除了 else 状态,只返回了 implode 函数,但他们都有正确的想法。感谢您的帮助。
【解决方案3】:
<?php
function trimTxt($str, $limit){ 
    /** remove consecutive spaces and replace with one **/
    $str = preg_replace('/\s+/', ' ', $str);

    /** explode on a space **/
    $words = explode(' ', $str);

    /** check to see if there are more words than the limit **/
    if (sizeOf($words) > $limit) {
       /** more words, then only return on the limit and add 3 dots **/
       $shortTxt = implode(' ', array_slice($words, 0, $limit)) . 'content here';
    } else {
       /** less than the limit, just return the whole thing back **/
       $shortTxt = implode(' ', $words);
    }
    return $shortTxt;
}
?>

【讨论】:

  • 请不要只写代码,向OP解释一下。
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 2011-10-16
相关资源
最近更新 更多