Forum Moderators: coopster

Message Too Old, No Replies

. after completion of a word

         

kkonline

6:05 am on Oct 26, 2007 (gmt 0)

10+ Year Member



Hi there, I am working on a search display part.
I have used

if (strlen($rows['content'])>100)
$rows['content'] = substr($rows['content'],0,100)."...";;
echo $rows['content'] . '<br>';
to restrict the display of more than 100 characters for displaying a short content.

The problem I am facing is that sometimes the words are cut in between as shown below

"Redistributions of source code must retain the a..."

I want a... to be a complete and not to break the work in middle

it should be "Redistributions of source code must retain the above..."

How to let the complete word show and then display ...

vincevincevince

6:55 am on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$snippet=preg_replace("/^.{1,100}(?![a-z])/i","",$text);

^ = start of string
. = any character
{1,100} = match as many characters as possible, but at least one and at most 100
(?!....) = negative look ahead read more [webmasterworld.com]
[a-z] = a-z
/i = case insensitive

kkonline

7:41 am on Oct 26, 2007 (gmt 0)

10+ Year Member



hi vince
i used
preg_replace("/^.{1,2}(?![0-9a-zA-Z])/i","...",$rows['content']);
echo $rows['content'];

It is not limiting the printed data instead it prints the whole content as

"Redistribution and use in source and binary forms, with or without modification, is permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution."

vincevincevince

11:05 am on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



{1,2} < this will try to match a string of between 1 and 2 characters length! Try changing it to {1,100}... and next; remember preg_replace returns the result not changes the input.

$snippet = preg_replace(........
print $snippet;

n.b. you don't need a-zA-Z if you use /i as /i makes it case insensitive