Skip navigation
Currently Being Moderated

truncate text to the end of a word

Apr 22, 2012 5:27 AM

i have made a truncate using the code i will shown by bregent, however i want to extend this to make sure the full word is shown rather than cutting off

 

code here

 

$query_Recordset1 = "SELECT tk_job_title, tk_job_location, tk_job_salary, LEFT(tk_job_desc, 80) as truncated_job_desc FROM think_jobsearch ORDER BY think_jobsearch.tk_job_title";

$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);

$Recordset1 = mysql_query($query_limit_Recordset1, $hostprop) or die(mysql_error());

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

 

 

and the result of the truncate is

 

<?php echo $row_Recordset1['truncated_job_desc']; ?>

 

what do i do to make sre a word is complete?

 

 

thanks in advance

 
Replies
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 22, 2012 6:18 AM   in reply to Jonathan Fortis

    At the moment, how many letters is it showing by default after you run your truncate function? 80?

     

    If it is showing 80 characters, change the highlited string in the following code. This will display 150 characters.

    $query_Recordset1 = "SELECT tk_job_title, tk_job_location, tk_job_salary, LEFT(tk_job_desc, 150) as truncated_job_desc FROM

     

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 22, 2012 6:19 AM   in reply to Jonathan Fortis

    Also, if you'd like to truncate to the nearest word, you should visit string position - strpos function. You can read about it here:

     

    http://www.ambrosite.com/blog/truncate-long-titles-to-the-nearest-whol e-word-using-php-strrpos

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 25, 2012 4:08 AM   in reply to Jonathan Fortis

    Yes.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 25, 2012 4:46 AM   in reply to Jonathan Fortis

    Yes. Your SQL query will return the entire data available in 'job_desc' and your strrpos statement will truncate it

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 25, 2012 10:33 PM   in reply to Jonathan Fortis

    Hi,

     

    Can you post your full PHP + HTML Code here so I can take a look at what might be going wrong and where.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 26, 2012 11:09 AM   in reply to Jonathan Fortis

    Can you upload your site and post a link here?

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 1:15 AM   in reply to Jonathan Fortis

    Can you define a variable in PHP to call your data from the job_desc table?

     

    Once you read the data from your desired column, assign it to a variable something like

     

    $jobdesc = "your-definition-of-the-variable-from-db;

    Then, you could use a function to truncate the called text.

     

    function truncateDesc($input, $numwords, $padding=""){

        $output = strtok($input, " \n"); while(--$numwords > 0) $output .= " " . strtok(" \n"); if ($output != $input) $output .= $padding; return $output;

    }

     

    Your next variable would be to truncate the data that is read using the previous function defined above:

    $truncated = truncateDesc($jobdesc, 10, "...");

     

    Then finally, once $truncate variable is defined, you could echo it in your HTML:

     

    echo "<p>$truncated</p>";

     

    A static example of this code will be as follows:

    <?PHP

    $longtext = "This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description. This is some test description.";

     

    function truncateLong($input, $numwords, $padding="")

    { $output = strtok($input, " \n"); while(--$numwords > 0) $output .= " " . strtok(" \n"); if($output != $input) $output .= $padding; return $output; }

     

    $truncated = truncateLong($longtext, 10, "...");

    echo "<p>$truncated</p>";

     

    ?>

    This should ideally work.

     

    Undo the previous strpos function that you've defined earlier before trying this.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 2:33 AM   in reply to Jonathan Fortis

    I meant define a variable that loads the data from job_desc field that is located inside a table in your database.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 4:49 AM   in reply to Jonathan Fortis

    Something like this:

     

    Your SQL code:

    mysql_select_db($database_testconnection, $testconnection);

    $query_Recordset1 = "SELECT job_desc FROM think_jobsearch";

    $Recordset1 = mysql_query($query_Recordset1, $testconnection) or die(mysql_error());

    $row_Recordset1 = mysql_fetch_assoc($Recordset1);

    $totalRows_Recordset1 = mysql_num_rows($Recordset1);

    In your main HTML body, if you give this:

    <?php echo $row_Recordset1['job_desc']; ?>

    You'll get all data in all rows from your table

     

    Now that we're able to echo the result in the page, we need to truncate it to achieve our objective. For this, look at the code below:

    <?PHP

    $description = $row_Recordset1['job_desc'];

     

    function truncateWords($input, $numwords, $padding="")

    { $output = strtok($input, " \n"); while(--$numwords > 0) $output .= " " . strtok(" \n"); if($output != $input) $output .= $padding; return $output; }

     

    $truncated = truncateWords($description, 10, "...");

    echo "<p>$truncated</p>";

     

    ?>

    This will cut it to 10 words and add ellipsis (...) to the text beyond 10 words.

     

    I guess you could figure out to explode the results from your recordset column into different rows and order them as per the Job_ID or something.

     

    Hope this helps.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 5:40 AM   in reply to Jonathan Fortis

    Post your current code here

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 6:02 AM   in reply to Jonathan Fortis

    The last line of your code has

     

    results <?php echo $row_Recordset1['job_desc']; ?>

    this is why it's displaying all data. Actually your truncate code is not doing its job.

     

    Try removing the last line and replace it with

    <?PHP

    $description = $row_Recordset1['tk_job_desc'];

    function truncateWords($input, $numwords, $padding="")

    { $output = strtok($input, " \n"); while(--$numwords > 0) $output .= " " . strtok(" \n"); if($output != $input) $output .= $padding; return $output; }

    $truncated = truncateWords($description, 10, "...");

    echo "<p>$truncated</p>";

    ?>

    See if it works

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 6:53 AM   in reply to Jonathan Fortis

    I know that. But, I asked you to change where it is placed. And to remove the last line with

    results <?php echo $row_Recordset1['job_desc']; ?>

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 7:13 AM   in reply to Jonathan Fortis

    Yes mate. You should remove that code from the top of your PHP page. It should be present at the bottom.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 2, 2012 5:20 AM   in reply to Jonathan Fortis

    As adviced, you already have the function present between lines 421 an 431. The same function between lines 2 and 11 are redundant. Remove these lines 2 till 11 and it should ideally work.

     

    Cheers,

    ST

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points