Forum Moderators: coopster

Message Too Old, No Replies

substr - Making it stop after a word?

         

internetheaven

7:00 am on Mar 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My own insite search function displays a small description of the page retrieved from the page itself. I use substr to set the amount of characters to use for that description:

$a[array2] = substr($a[array1], 0, 200)." ...";

Generally it cuts off in the middle of a word which is why I use the "..." I really think it would look more professional and more pleasing to the eye if it didn't cut off mid-word/mid-sentence.

Is there anything that will go to the number of characters specified but then go on to include everything up until the next full stop? Or at least the next space so that it does not cut off in the middle of words.

ogletree

8:01 am on Mar 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should be able to find the last space in your string then only return everything before it. Maybe using the strrchr and substr or something like that.

grandpa

8:08 am on Mar 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want to avoid cutting off words, maybe load each word from array1 into another array, and use word counts vs character count. Estimating the length of the snippet with word counting will take some tweaking, and will probably always result in varying snippet lengths.

To avoid mid-sentence, apply the same principle, except with complete sentences, as determined by the placement of a period. That won't work on sites where people don't use proper sentence structure.

Actually, for either of those methods, you could then use strlen to decide if you want to add or remove a bit of text.