Silverstripe 创建更好的 LimitWordCount 函数

问题描述 投票:0回答:1

框架在 StringField.php 文件中提供 LimitWordCount。它很棒,但并不完全完美。一些标签和标点符号需要更正。 我想要 :

  • 保留标签
    <sup><sub><em>
  • 正确的标点符号:«.»到 ”。 “, “,“ 到 ”, ”, ”?”到 ”? » 等等....

我做了自己的LimitWordCount:

public function MyLimitWordCount($int) {

    $text = strip_tags($this->owner->value, '<sup><sub><em>');

     if (str_word_count($text, 0) > $int) {

          $words = str_word_count($text, 2);
          $pos = array_keys($words);
          $text = substr($text, 0, $pos[$int]) . '…';
     }

    $text = str_replace('.', '. ', $text );
    $text = str_replace('!', '! ', $text );
    $text = str_replace('…', '… ', $text );
    $text = str_replace('?', '? ', $text );

    return $text;

}

这并不完全有效。一些标点符号未更正,并且在 HTMLEditor 字段中制作的视频短代码

[embed width="480...]
与我的功能保持一致。我不知道他做错了什么。

php silverstripe strip
1个回答
0
投票

您将继续在文本中获取短代码,因为您需要先解析它们。

使用

(string)$this->owner->dbObject('value')
而不是
$this->owner->value
最容易解决此问题。

然后您可以操作短代码已展开的值。

我无法评论为什么在这种情况下你的标点符号没有被“纠正”,因为你没有提供关于哪些标点符号以及在什么情况下它不起作用的太多信息。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.