473,802 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2405
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******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
Jul 17 '05 #2

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

Similar topics

1
5413
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 is required to be installed and if not, which version of the program database needs to be installed with the current version of Access. (hope that makes sense). If anyonoe could point me in the right direction, I'd really appreciate it.
18
93382
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
5724
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
1
1496
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 examples, and have created an online demonstration that can process XML into XString. My goal in annoucing/previewing the technology is to get comments and feedback from others to inspire and guide future efforts. Online at...
12
5758
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 identify emails from a user. (the string will be in the subject line of the email). I'm trying to figure out which approach I should use to encrypt the data. The string will be less than 20 characters long, and I'd like the encrypted version to be...
4
1808
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 say: class A {
17
3478
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; while( data != *((short*) "DA") ) j++; But this doesn't work. The char obviously has an equivalent 16-bit value so how do I get that info in a simple way?
16
3934
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. My problem is that when I pull the data (currently stored as a dictionary on the site) off the site, it is a string, I can use eval() to make that string into a dictionary, and everything is great. However, this means that I am using eval() on...
0
1456
by: Kerem Gümrükcü | 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 terribly slow and the preview is unbelievable slow, especially when you have a 70 page data you want to print. I use a PDF generator to print the data, at first for testing. Not the printig is slow, but the
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.