473,241 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Safely cut off short preview version of long string

Hi all,

Here`s a problem I have been working on for a while, but can`t seem to
solve satisfactory.

I have a database with blog entries. Because each of those entries has a
variable length which can be quite long, I want to build an overview page.
Of each entry there will be a preview version, say 700 characters max.

My problem has to do with HTML tags. If for example an entry contains a
<BLOCKQUOTE> with a large quote, my function would break off somewhere
halfway in the quote. The end result of course won`t have the
</BLOCKQUOTE>, rendering the resulting page horribly bad.

I would like to build a function that breaks a string up to max X
characters long, but plays it safe when it encounters any HTML tag: it
does not matter if the end result is a string of say 670 characters long,
it only matters that it approximates the max character setting and doesn`t
mess up the HTML tags.

Can anyone point me in the right direction?

Hans
Jul 17 '05 #1
1 2382
Following on from Hans Gruber's message. . .
My problem has to do with HTML tags. If for example an entry contains a
<BLOCKQUOTE> with a large quote, my function would break off somewhere
halfway in the quote. The end result of course won`t have the
</BLOCKQUOTE>, rendering the resulting page horribly bad.

I would like to build a function that breaks a string up to max X
characters long, but plays it safe when it encounters any HTML tag: it
does not matter if the end result is a string of say 670 characters long,
it only matters that it approximates the max character setting and doesn`t
mess up the HTML tags.


A simple way would be to decide where your end point was going to be
roughly (not inside <...>) then leave all the remaining tags but remove
the text.

The reason for putting all the following tags in is that you can have
complex nested structures where you'd have to do lots of complicated
parsing - just not worth the effort. Also the entry could start with
say <center> and end with </center> many pages apart.
eg.
1 - split string to get 1st X chars and work with remainder of string
2 - explode remainder by '<' so that tags _except possibly in array[0]_
will be the first part and therefore look like "ATAG>some text" (or
"/ATAG>some text")
3 - if array[0] doesn't contain a '>' this is tail of a tag
(NB /sort of/ there are two exceptions - no more tags at all and this
tag followed immediately by another in which case '>' would appear as
last character if you see what I mean)
4 - Now strip the bits after '>' from the array , implode with '<' and
add to end of text.

--
PETER FOX Not the same since the pancake business flopped
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: RWC | last post by:
Hi Folks, I'm looking for a way to determine if the client machine has access installed and if so, what version. The reason I need this is to determine (programatically) if the Access Runtime...
18
by: a | last post by:
short s; long l; s= -2; l= -3; printf("% _ %_",s, l); What characters should be filled out in the formatted string for output? Thanx
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
1
by: wgilreath | last post by:
Announcing a Preview of XString, an XML technology - XML as a String. I've created a means to efficiently encode XML as a flat string, which I term XString. I've written a paper, put some...
12
by: erikcw | last post by:
Hi, I'm trying to devise a scheme to encrypt/obfuscate a short string that basically contains the user's username and record number from the database. I'm using this encrypted string to...
4
by: tech | last post by:
Hi, I need to pass a block of say 320 bytes memory between some classes which do various processing on it. The app needs to be quick so i can't keep copying. The simplest way is via pointer...
17
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0;...
16
by: Fett | last post by:
I am creating a program that requires some data that must be kept up to date. What I plan is to put this data up on a web-site then have the program periodically pull the data off the web-site. ...
0
by: Kerem Gmrkc | last post by:
Hi, i use the code from this code sample on MSDN: for printing a 5 and sometimes 70 page text-only data: http://msdn.microsoft.com/en-us/library/ms404294.aspx The point is, that this is...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.