truncate strings
-
Hey guys, My website contains a messagebox where visitors can (obviously) post messages. The right side of my website contains an area where the latest three messages are shown. Now I want to truncate these messages to a maximum of 150 characters (if the're shorter, leave them alone). This is easy, not a problem, however, the string can contain image tags (smiley's) now when truncating the string, I want to leave those image tags alone and not truncate in the middle of a tag. I want to remove all html tags (which in fact can only be image tags) then truncate the text to 150 chars as needed and restore the image tags at the original position if needed. Anyone a good approach or idea? I change the image tokens which mean a smiley into image tokens before I store them in the database, this prevents me from checking for image tokens each time I read the message from the database. If I don't convert the tokens into image tags before storing, the problem still exists because a token may be multiple characters (for example a semicolon and a ) sign for a wink smiley) and I don't want to truncate the string in the middle of a token leaving me with a semicolon only.
I love it when a plan comes together http://www.zonderpunt.nl[^]
-
Hey guys, My website contains a messagebox where visitors can (obviously) post messages. The right side of my website contains an area where the latest three messages are shown. Now I want to truncate these messages to a maximum of 150 characters (if the're shorter, leave them alone). This is easy, not a problem, however, the string can contain image tags (smiley's) now when truncating the string, I want to leave those image tags alone and not truncate in the middle of a tag. I want to remove all html tags (which in fact can only be image tags) then truncate the text to 150 chars as needed and restore the image tags at the original position if needed. Anyone a good approach or idea? I change the image tokens which mean a smiley into image tokens before I store them in the database, this prevents me from checking for image tokens each time I read the message from the database. If I don't convert the tokens into image tags before storing, the problem still exists because a token may be multiple characters (for example a semicolon and a ) sign for a wink smiley) and I don't want to truncate the string in the middle of a token leaving me with a semicolon only.
I love it when a plan comes together http://www.zonderpunt.nl[^]
Hi I have one idea but I am not sure whether it is possible. You need to break words not letters (or any characters) as smileys will not contain spaces between each character. So you can truncate your messages to a maximum number of words. Assume word can have a maximum 30 characters and truncate 10 words maximum. Hope that will be perfect to display in your web pages.
Rate this message. Thank you. Harini :)
-
Hi I have one idea but I am not sure whether it is possible. You need to break words not letters (or any characters) as smileys will not contain spaces between each character. So you can truncate your messages to a maximum number of words. Assume word can have a maximum 30 characters and truncate 10 words maximum. Hope that will be perfect to display in your web pages.
Rate this message. Thank you. Harini :)
In fact your solution gives my an idea which will furfill my needs... I can count till the desired number of characters, let's say 140 and then truncate the string as soon as the next space occures, or if character 140 is in the middle of a html tag, as soon as the tag ends.... Thanks for bringing up the lighbulb above my head :doh:
I love it when a plan comes together http://www.zonderpunt.nl[^]
-
In fact your solution gives my an idea which will furfill my needs... I can count till the desired number of characters, let's say 140 and then truncate the string as soon as the next space occures, or if character 140 is in the middle of a html tag, as soon as the tag ends.... Thanks for bringing up the lighbulb above my head :doh:
I love it when a plan comes together http://www.zonderpunt.nl[^]
Yeah that is correct. You are most welcome
Rate this message. Thank you. Harini :)