Jump to content

Recommended Posts

Hi everyone,

 

I am trying to truncate text from stories that I'm pulling from a database. However, the code below doesn't seem to be truncating them at all.

 

Here is the function:

function myTruncate2($string, $limit, $break=" ", $pad="...")
  {
    // return with no change if string is shorter than $limit
    if(strlen($string) <= $limit) return $string;

    $string = substr($string, 0, $limit);
    if(false !== ($breakpoint = strrpos($string, $break))) {
      $string = substr($string, 0, $breakpoint);
    }

    return $string . $pad;
  }

Here's the code that is generating the HTML:

<?php 
$summary = blurb(1);
$shortdesc = myTruncate2($summary, 10);
echo "<p>$shortdesc</p>";
?>

 

This is displaying full stories, the same as I would get with blurb(1) or echo $summary

 

blurb() is a function that returns the summary of our stories, so that blurb(4) would return the text of the story with key = 4 in the MySQL database.

 

I noticed that when I try strlen($shortdesc), it returns 0.

 

Any suggestions? Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/191104-truncating-text-returned-by-function/
Share on other sites

Just tried this:

 

<?php

$summary = "some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. x";

$trunc = myTruncate2($summary, 10);

echo $trunc;

/*
Outputs: 
some tideo...
*/
function myTruncate2($string, $limit, $break="", $pad="...")
  {
    // return with no change if string is shorter than $limit
    if(strlen($string) <= $limit) return $string;

    $string = substr($string, 0, $limit);
    if(false !== ($breakpoint = strrpos($string, $break))) {
      $string = substr($string, 0, $breakpoint);
    }

    return $string . $pad;
  }

?>

 

And it worked as expected. Perhaps your blurb function is echoing the content instead of returning it as a string. To capture the echo look into ob_start and ob_get_clean to add around the $summary = portion. Or, the more preferred method, modify the blurb function to return the summary and not echo it.

That is exactly the issue. Going to try to figure this out now. The following is the blurb() function from the functions.php file that we're including:

function blurb($storyId)
{
   $query = "SELECT blurb FROM `testdate` WHERE story_id = $storyId";
   $result = mysql_query($query);

   if ($result)
   {
      $row = mysql_fetch_assoc($result);
      echo $row['blurb'];
  
   }

 

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.