Jump to content

Displaying only part of a string


Aftermath

Recommended Posts

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.

[code]
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
}
?>
[/code]

I was wondering how would I go about making it only display, say the first 150 characters of the enrty?
Link to comment
https://forums.phpfreaks.com/topic/9889-displaying-only-part-of-a-string/
Share on other sites

Try this in your script

[code]<?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);

?> [/code]

returns

hello there. This .....

Thanks a lot! It worked ^_^

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

[code]
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'];
[/code]
Ok, here we go, line by line...

[code]
function truncate_string($details,$max)
{
[/code]

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.

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

[code]
        $details = substr($details,0,$max);
[/code]
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 =).

[code]        $i = strrpos($details," ");
[/code]
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.

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


[code]
        $details = $details." ..... ";
    }
[/code]
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.

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

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

Hope this isn't too muddy.

Lite...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.