473,231 Members | 1,806 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,231 software developers and data experts.

Poblem when creating short description of text

200 100+
Initialy i wanted to create a short description of a text to be shown as preview, I found the following which worked OK
Expand|Select|Wrap|Line Numbers
  1. $length = 35; // The number of words you want
  2. $words = explode(' ', $b_keimeno); // Creates an array of words
  3. $true_length=count($words); //to see if description smaller than $length
  4. $words = array_slice($words, 0, $length);
  5. $s_keimeno = implode(' ', $words);
  6. if ($true_length>$length)
  7. $s_keimeno.="...";    
Now however the writer start writting with html tags to make bold,links etc. as a result short preview sometimes cuts before clossing a tag or worse inside an opening tag like <a href=... and the next element after this short description appears weird. Any ideas?
Dec 10 '08 #1
2 2408
5,821 Expert 4TB
Heya, mikek12004.

One option is to use strip_tags() to remove any HTML from the preview text (PHP: strip_tags - Manual).

Short of writing your own string parser, though, I am not aware of any good solution for counting words outside of HTML tags (though I am working on one!).
Dec 10 '08 #2
938 Expert 512MB
One solution I used on a project recently was a stored procedure. Now please don't all shout at me normally I'm not a fan but this seemed to work a treat. Also I was editing someone else's code and this was simplest.

So what did I do?

Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2.   DECLARE iStart, iEnd, iLength int;
  3.     WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
  4.       BEGIN
  5.         SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
  6.         SET iLength = ( iEnd - iStart) + 1;
  7.         IF iLength > 0 THEN
  8.           BEGIN
  9.             SET Dirty = Insert( Dirty, iStart, iLength, '');
  10.           END;
  11.         END IF;
  12.       END;
  13.     END WHILE;
  14.     RETURN Dirty;
  15. END
I was using a MySQL database. Then in the code (ASP unfortunately) I called the stored procedure on the full text. I have a site with many categorised pages and the category landing page display the first n characters. so it's character based which can leave you cut up in the middle of a word.

Anyway, you may be able to tweak it to be word based. Otherwise the simple strip-tags would work a treat. A function that has no counter-part in ASP, at least not one I could find.

Still thought I'd share my thoughts.

Cheers and Merry Christmas!
Dec 19 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
by: Orloff | last post by:
Hi, on most (all?) browsers, when you put the pointer on a <a href="..." title="popup text">this is a link</a> without clicking on the link, there is a popup caption with the "popup text". I...
by: Barbara Lindsey | last post by:
Thank you for your help on the trigger question. The RULE worked for most of the cases I had for this, but I have one that is giving me trouble. Here are my table definitions: CREATE SEQUENCE...
by: Dixie | last post by:
I wish to add some fields to an existing table in code. I am using the following code from rkc. CurrentDb.Execute ("ALTER TABLE MyTable ADD MyNewField Text 25") This works , but I need to also set...
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
by: Xh | last post by:
Hi all, I have successfully installed DB2 Net Search Extender. DB2Ext is up and running, I've run successfully this command: db2text ENABLE DATABASE FOR TEXT CONNECT TO TEST1; but when I...
by: DavidPr | last post by:
I'm creating (trying to create) a picture gallery for my website. The script is not working. I've been working on it now for about 80 hours with no success. My php skills aren't very good. This...
by: GloStix | last post by:
Okay, when I click the link it dissapears and makes a white space underneath it but the space is clickable. so when you click the space, the link actually works Used Firefox 3.0 RC2 for this. OS X...
by: mlthomas007 | last post by:
Hi, A client asked for a code book (all fields, descriptions, tables, etc.) from our Access database. Has anyone had to do this? It seems to me there must be a way to extract all this...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
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...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
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...
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...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.