Coldfusion 中是否有函数可以从字符串中仅获取 2 行文本?

发布于 2024-10-20 20:30:04 字数 150 浏览 2 评论 0原文

我知道这适用于其他语言,但想看看是否有现有的代码/函数。

该字符串可以由许多不同的查询填充,但它们都需要以相同的方式、相同的长度等显示。

我有一个函数,可以通过字数控制字符串长度,但我更愿意确保我至少有最多 2 句话或 2 行文字。

谢谢

I know this works in other languages, but wanted to see if there is existing code/functions.

This string can be populated from numerous different queries, but they need to be all displayed the same way, same length etc.

I have a function, to control string length by word count, but I would prefer to make sure that I have at least 2 sentences or 2 lines of text at most.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃→可爱长大的 2024-10-27 20:30:04

我的工作中有一个类似的任务,你必须选择一个任意数字,看起来你选择了 190。话虽这么说,你不能只是希望字符/单词返回是相关的。您必须确保它们是您关心的事情,这就像您确实在查看您的评论一样。

尝试在字符串中查找关键字并使用 mid() 函数获取关键字两侧的一定数量的字符:

<cfscript>
  max_chars = 190;
  full_article = #the full article#;
  keyword_position = find(keyword, full_article);

  if( keyword_position != 0 ) {
    excerpt = mid(full_article, 
      keyword_position - max_chars / 2 - len(keyword_position) / 2,
      max_chars);
  }
</cfscript> 

...或类似的内容。我将把它留给您,以确保您不会尝试在 full_article 开始之前或结束之后获取字符,并添加省略号和其他内容。

I had a similar task at my job and you have to pick an arbitrary number, and it looks like you've chosen 190. That being said, you can't just hope that the characters/words returned are relevant. You have to ensure that they are if its something you care about, which is seems like you do looking at your comments.

Try to find the keyword in the string and use the mid() function to get a certain number of characters on either side of the keyword:

<cfscript>
  max_chars = 190;
  full_article = #the full article#;
  keyword_position = find(keyword, full_article);

  if( keyword_position != 0 ) {
    excerpt = mid(full_article, 
      keyword_position - max_chars / 2 - len(keyword_position) / 2,
      max_chars);
  }
</cfscript> 

...or something like that. I'll leave it to you to make sure that you're not trying to get characters before the start of the full_article, or after the end of it, and adding ellipses and stuff.

情栀口红 2024-10-27 20:30:04

尝试类似 fullLeft 或深入研究 CFLib 上的其他字符串操作 UDF。如果您正在寻找更具体的东西,您可以向我们展示另一种语言的类似功能,我们会更好地向您指出类似的东西。

Try something like fullLeft or dig through the other string manipulation UDFs at CFLib. If you're looking for something more specific could you show us a comparable function in another language and we'd be better able to point you to something similar.

情深如许 2024-10-27 20:30:04
_TestString = "I know this works in other languages, but wanted to see if there is existing code/functions. This string can be populated from numerous different queries, but they need to be";
if ( len(_TestString) GT 190)
{
  _TestString = Left(_TestString,190) & "...";
}

这将输出:

我知道这适用于其他语言,但想看看是否有现有的代码/函数。该字符串可以由许多不同的查询填充,但它们需要全部显示...

您可能不想做更多的事情,字符串操作可能会无缘无故地变得昂贵,您不应该浪费处理除非必须,否则显示层。

_TestString = "I know this works in other languages, but wanted to see if there is existing code/functions. This string can be populated from numerous different queries, but they need to be";
if ( len(_TestString) GT 190)
{
  _TestString = Left(_TestString,190) & "...";
}

That will output:

I know this works in other languages, but wanted to see if there is existing code/functions. This string can be populated from numerous different queries, but they need to be all displayed t...

You probably don't want to do anything more than that, string manipulation can get expensive for no reason, you shouldn't waste processing on the display layer unless you have to.

何止钟意 2024-10-27 20:30:04

CFLIB 提供了大量的字符串操作函数。您可能会发现 abbreviate() 很有用,尤其是对于搜索结果:http://cflib.org/udf/abbreviate

<cfscript>
/**
* Abbreviates a given string to roughly the given length, stripping any tags, making sure the ending doesn't chop a word in two, and adding an ellipsis character at the end.
* Fix by Patrick McElhaney
* v3 by Ken Fricklas [email protected], takes care of too many spaces in text.
* 
* @param string      String to use. (Required)
* @param len      Length to use. (Required)
* @return Returns a string. 
* @author Gyrus ([email protected]@norlonto.net) 
* @version 3, September 6, 2005 
*/
function abbreviate(string,len) {
    var newString = REReplace(string, "<[^>]*>", " ", "ALL");
    var lastSpace = 0;
    newString = REReplace(newString, " \s*", " ", "ALL");
    if lenn(newString) gt len) {
        newString = left(newString, len-2);
        lastSpace = find(" ", reverse(newString));
        lastSpace = len(newString) - lastSpace;
        newString = left(newString, lastSpace) & " &##8230;";
    }    
    return newString;
}
</cfscript>

CFLIB has plenty of string manipulation functions on offer. You may find abbreviate() is useful, especially for search results: http://cflib.org/udf/abbreviate

<cfscript>
/**
* Abbreviates a given string to roughly the given length, stripping any tags, making sure the ending doesn't chop a word in two, and adding an ellipsis character at the end.
* Fix by Patrick McElhaney
* v3 by Ken Fricklas [email protected], takes care of too many spaces in text.
* 
* @param string      String to use. (Required)
* @param len      Length to use. (Required)
* @return Returns a string. 
* @author Gyrus ([email protected]@norlonto.net) 
* @version 3, September 6, 2005 
*/
function abbreviate(string,len) {
    var newString = REReplace(string, "<[^>]*>", " ", "ALL");
    var lastSpace = 0;
    newString = REReplace(newString, " \s*", " ", "ALL");
    if lenn(newString) gt len) {
        newString = left(newString, len-2);
        lastSpace = find(" ", reverse(newString));
        lastSpace = len(newString) - lastSpace;
        newString = left(newString, lastSpace) & " &##8230;";
    }    
    return newString;
}
</cfscript>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文