Jump to content

Limit characters in php


Kaboom

Recommended Posts

Okay so as you can see here:

 

http://www.stephanddarryn.com/blog/2010/animals/index.php

 

The description doesnt have a character limit, i need it to show only 300 characters bfore it puts... then cut the rest of the desctiption off so they have to view the whole thing to see the whole thing. How would I do this? I need character limit at 300 then if its over that put ... and cut the rest out and then that's it if its lower don't add the ...

Link to comment
Share on other sites

Okay so as you can see here:

 

http://www.stephanddarryn.com/blog/2010/animals/index.php

 

The description doesnt have a character limit, i need it to show only 300 characters bfore it puts... then cut the rest of the desctiption off so they have to view the whole thing to see the whole thing. How would I do this? I need character limit at 300 then if its over that put ... and cut the rest out and then that's it if its lower don't add the ...

 

This should do the job more or less:

$string = "Bla bla bla bla bla bla bla bla bla bla bla bla bla";

function shorten($string, $length) {
if (strlen($string) > $length) {
	$string = substr($string, 0, $length)."...";
}
return $string;
}

$shortdesc = shorten($string, 300);
echo "$shortdesc"; //Outputs something 300 characters then ...

 

EDIT: I'd recommend you look at strlen and substr they're handy string functions, if you know how to use them.

Link to comment
Share on other sites

It didnt work :'( Now it doesnt show them at all... hold on ill try something else :D

 

How does it not work? If you look at a sample of the code:

$string = 'abcdefghijklmnopqrstuvwxyxz'; //long string
//[snip]//
$shortdesc = shorten($string, 5);

 

It'll output exactly: 'abcde...'

 

How are you implementing it?

Link to comment
Share on other sites

The way it work is it connects

        $posts = mysql_query("SELECT * FROM databasenamehere ORDER BY id", $db_id); 

 

then prints utput as:

$x = 1;

while ( $blogpost = mysql_fetch_array( $posts ) ) {
    if ( $x != 1 ) {
        echo "<hr>";
    }

    echo "<img src=\"http://www.elegantthemes.com/preview/eNews/wp-content/themes/eNews/timthumb.php?src=http://www.elegantthemes.com/images/sharedthumbs/8.jpg&h=74&w=73&zc=1\" alt=\"Animals\" width=\"73px\" height=\"74px\" />
						<h2><a href=\"article.php?id=$blogpost[id]\">$blogpost[subject]</a></h2>

						<p class=\"info\"><em>author</em>: $blogpost[poster]</p>
<p>$blogpost[content]</p>";
    $x++;
} 

 

$blogpost[content] is the one that displays the content of the post or description

 

so i need to limit $blogpost[content] but when i set $string = blogpost[content]; then it makes it display nothing and wierd. it set to load content from every post rather then have certain defined content and array all the posts onto that page. I also need to make is so when it hits 50 posts a page it adds buttons for pages at bottom like:

 

<< < Page [1] 2 3 4 ... of 7 > >>

 

thanks for what you've gave me but it's not working for this. When i managed to implement it it didnt show the post with under 300 at all

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.