Jump to content


Photo

Displaying only part of a string


  • Please log in to reply
4 replies to this topic

#1 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 18 May 2006 - 12:07 AM

Hello, I'm working on creating a blog and I need a hand.
Right now, I have the entry being called from the database and being displayed in a table.

while($row = mysql_fetch_array($result)) {

    $date = date("l F d Y", $row['timestamp']);

    $title = $row['title'];
    $entry = $row['entry'];
    $name = $row['username'];
    $id = $row['id'];

    ?> 

<table width="100%" align="left">
<tr><td><u> <?php echo $title; ?></u></td>
<td align="right"> <?php echo $date; ?></td></tr>
<tr><td colspan="2"><b><?php echo $name; ?></b></td></tr>
<tr><td colspan="2"> <?php echo $entry; ?></td></tr>
<tr><td colspan="2"><br /></td></tr>
<tr><td colspan="2"><a href="blog_entries.php?id=<?php echo $id; ?>">View Post</a></td>
<tr><td colspan="2"><HR WIDTH="100%"></td></tr>
</table>
<br />

    <?php
}
?>

I was wondering how would I go about making it only display, say the first 150 characters of the enrty?

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 18 May 2006 - 12:16 AM

Try this in your script

<?PHP

function truncate_string($details,$max)
{
    if(strlen($details)>$max)
    {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details." ..... ";
    }
    return $details;
}

$text = truncate_string("hello there. This is a long string",19);

?>

returns

hello there. This .....


all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 18 May 2006 - 12:25 AM

Thanks a lot! It worked ^_^

Could you possibly explain it so I know what it means?

function truncate_string($details,$max)
{
    if(strlen($details)>$max)
    {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details." ..... ";
    }
    return $details;
}

while($row = mysql_fetch_array($result)) {

    $date = date("l F d Y", $row['timestamp']);

    $title = $row['title'];
    $entry = truncate_string($row['entry'],20);
    $name = $row['username'];
    $id = $row['id'];


#4 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 18 May 2006 - 02:48 AM

Ok, here we go, line by line...

function truncate_string($details,$max)
{

obviously, this first line says "let's make a function named truncate_string; and it will need two pieces of information to do its job. The first is a variable named $details which contains the string/text we want to truncate/shorten. $max is the maximum length of text we want from the string.

    if(strlen($details)>$max)
    {
This line checks to make sure that the original string is longer than the max length we are looking to create.

        $details = substr($details,0,$max);
This line takes a slice off of the original string ($details). the slice starts at the beginning (the 0) and is $max characters long. The original string/text is then replaced with the slice ($details =).

        $i = strrpos($details," ");
This line checks to see where the last space is located in our new string. The reason for this is to make sure we didn't cut a word in the middle.

        $details = substr($details,0,$i);
This line uses the information from the previous line to actually make the string end with a full word.


        $details = $details." ..... ";
    }
This line simply adds some periods to the end of our new string to indicate to a reader that there is more text to follow.

    return $details;
}
This last line of our function simply passes our new string back to our script

$text = truncate_string("hello there. This is a long string",19);
This demonstrates how you could use the function in a script.

Hope this isn't too muddy.

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#5 Aftermath

Aftermath
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 18 May 2006 - 03:13 AM

That makes sense. The 'i' part was a little confusing to me before. Thanks for all the help mate :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users