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

Poblem when creating short description of text

mikek12004
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.="...";    
  8.  
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 2416
pbmods
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
nathj
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
  16.  
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!
nathj
Dec 19 '08 #3

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

Similar topics

0
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...
8
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...
5
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...
1
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...
5
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...
0
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...
2
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...
9
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...
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...

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.