471,624 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 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 2360
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

Post your reply

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

Similar topics

5 posts views Thread by Barbara Lindsey | last post: by
1 post views Thread by Dixie | last post: by
7 posts views Thread by mlthomas007 | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.