String trouble

Hi everyone,

I use this code to create a short text of 90 characters and than add three dots.

text = _root.shorttext1;
short = new String(text);
subshort = short.substr(0, 90)+" …";

Problem is that i don’t want to cut off the text in the middle of a word. How can I make sure flash never leaves half a word, but instead makes the string shorter so that it ends on a space character.

So instead of “word word word word word word word wo…”
I want “word word word word word word word …”

thanks in advance

S

i dont know if its possible, but i was thinking about his:

if you slice the string into an array, with the spaces being the cutters, so you have an array with all words in it

words[1] being the first word of your text etc etc

and then if you want to only view the first 10 words, and after that adding … to it, you should go with this:


for(i=1;1<=X;i++){
text += words*+" ";
}
text += " ...":

X being the number of words you want

should be something like that, all you need to know now is, how to slice an string to an array…

Problem is my design has only room for 90 chars. If I would allow the first 12 words, they could contain more than 90 letters…

the setup:
Each small block of text is set next to a thumbnail. Then people can click the thumb and then get a larger image, accompanied by the whole text…

But anyway: thanks for trying !

i found an answer, check back in 15 minutes

wow man,
that’s great.

** READ NEXT POST **


// this is the text
myText = "Problem is that i don't want to cut off the text in the middle of a word. How can I make sure flash never leaves half a word, but instead makes the string shorter so that it ends on a space character.";
// slice it back to 90 characters
newText = myText.substr(0,90);
// put thos characters in an array
Words = newText.split(" ",99);
//loop tru the array, not including the last word
for(i=0;i<Words.length-1;i++){
	// put the word including a space into a new var
	shortText += Words*+" ";
}
// add 3 dots at the end
shortText += "...";
//and there you have it
trace(shortText)

there ya go :smiley:

this is the correct code:


// this is the text
myText = "Problem is that i don't want to cut off the text in the middle of a word. How can I make sure flash never leaves half a word, but instead makes the string shorter so that it ends on a space character.";
// slice it back to 90 characters
newText = myText.substr(0,87);
// put thos characters in an array
Words = newText.split(" ",99);
//loop tru the array, not including the last word
for(i=0;i<Words.length-1;i++){
        // put the word including a space into a new var
        shortText += Words*+" ";
}
// add 3 dots at the end
shortText += "...";
//and there you have it
trace(shortText)

this one will always return 90 characters, the previeus one, would return 93… :slight_smile: (the 3 dots ;))

Looks christalclear to me. awesome man…
I owe you one

stanley

just send me 200 euro’s to this back #: 112561616 :stuck_out_tongue:

makeList = function(){
for(i=2;i<=_root.projectcount;i++) {
duplicateMovieClip(“serviceunit”, “serviceunit”+i, i);
unit_i = eval(“serviceunit”+i);
unit_i.id = eval("_root.id"+i);
unit_i._y = [code to determine _y value]
unit_i.subclip.title = eval("_root.title"+i);

            _root.maxdepth = unit_i.getDepth();

           //the string part ... 
  myText = eval("_root.shorttext"+i);
           // slice it back to 90 characters
  newText = myText.substr(0,87);
           // put thos characters in an array
  Words = newText.split(" ",99);
          //loop tru the array, not including the last word
	for(t=0;t&lt;Words.length-1;t++){               

// put the word including a space into a new var
subshort += Words[t]+" ";
}
// add 3 dots at the end
subshort += “…”;
//

          unit_i.subclip.shorttext = subshort;
          unit_i.subclip.thumbcontainer.loadMovie("portfolio/thumb_"+unit_i.id+".jpg");
			
} 
}

makeList();


Dammit. I’m not in the clear yet :hangover: There’s a conflict going on here. With the code above, every duplicated (thumbnail-) movieclip gets the same tekst.
All the original texts are on the root and are named like this: _root.shorttext1
_root.shorttext2
_root.shorttext3
(dynamically loaded from an external file)

Am I missing something here?

omg, this is really messy


makeList = function () {
	for (i=2; i<=_root.projectcount; i++) {
		duplicateMovieClip("serviceunit", "serviceunit"+i, i);
		unit_i = eval("serviceunit"+i);
		unit_i.id = eval("_root.id"+i);
		unit_i._y = 20; // edit this
		unit_i.subclip.title = eval("_root.title"+i);
		_root.maxdepth = unit_i.getDepth();
		// this is the text
		myText = eval("_root.shorttext"+i);
		// slice it back to 90 characters
		newText = myText.substr(0, 87);
		// put thos characters in an array
		Words = newText.split(" ", 99);
		// loop tru the array, not including the last word
		for (i=0; i<Words.length-1; i++) {
			// put the word including a space into a new var
			subshort += Words*+" ";
		}
		// add 3 dots at the end
		subshort += "...";
		// and there you have it
		unit_i.subclip.shorttext = subshort;
		unit_i.subclip.thumbcontainer.loadMovie("portfolio/thumb_"+unit_i.id+".jpg");
	}
};
makeList();

this is without errors

and to make it more clear, here’s an function to cut the text


cutText = function (x) {
	myText = x;
	newText = myText.substr(0, 87);
	Words = newText.split(" ", 99);
	for (i=0; i<Words.length-1; i++) {
		subshort += Words*+" ";
	}
	subshort += "...";
	return subshort
};
makeList = function () {
	for (i=2; i<=_root.projectcount; i++) {
		duplicateMovieClip("serviceunit", "serviceunit"+i, i);
		unit_i = eval("serviceunit"+i);
		unit_i.id = eval("_root.id"+i);
		unit_i._y = 20;
		unit_i.subclip.title = eval("_root.title"+i);
		_root.maxdepth = unit_i.getDepth();
		unit_i.subclip.shorttext = cutText(eval("_root.shorttext"+i));
		unit_i.subclip.thumbcontainer.loadMovie("portfolio/thumb_"+unit_i.id+".jpg");
	}
};
makeList();

including ur script

It’ll take me some time to make txtfiles to replace database information and clean up the fla a little. It’s also part of another movie. You’ll almost need the whole site or you’ll see nothing…

I’ll get back with this…

Wow…
again way much more than what i bargained for :slight_smile:
you rock !
thanks

np dude… its just an hobby