macromedia.coldfusion.advanced_techniques
[Top] [All Lists]

Splitting a text string

Subject: Splitting a text string
From: "tomtomtom"
Date: Mon, 17 Nov 2008 21:43:06 +0000 UTC
Newsgroups: macromedia.coldfusion.advanced_techniques

Hello all

 I'm trying for some time to split a text string into several parts without 
cutting words apart. Some time ago I found a great UDF but it doesn't exactly 
do what I need (see code). It only displays the amount of words I set in 
'words'.

 Let's assume that I've got a text string with 1203 chars. I would love to set 
a variable charcount = 400. At the very end I would like to have four variables 
containing each (about) 400 chars and the last one containing the last 3 chars 
(approx). The function should not cut words apart (that's why I used brackets 
before). And the diamond would be that the function would tell me how many 
variables (containing each about 400 chars) it built.

 Did somebody understand what I mean?

 Many thanks for inputs

 function getWords(str,words) {
                var numWords = 0;
                var oldPos = 1;
                var i = 1;
                var strPos = 0;
                
                str = trim(str);
                str = REReplace(str,"[[:space:]]{2,}"," ","ALL");
                numWords = listLen(str," ");
                if (words gte numWords) return str;
                for (i = 1; i lte words; i=i+1) {
                        strPos = find(" ",str,oldPos);
                        oldPos = strPos + 1;
                }
                if (len(str) lte strPos) return left(str,strPos-1);
                return left(str,strPos-1) & " ...";
        }


<Prev in Thread] Current Thread [Next in Thread>