Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

truncate text to the end of a word

Engaged ,
Apr 22, 2012 Apr 22, 2012

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

TOPICS
Server side applications
4.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 22, 2012 Apr 22, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 22, 2012 Apr 22, 2012

hi, i know how to chanhe the amount of characters showing i need to know how to show complete words rather than breaking them of half half way throught the word

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 22, 2012 Apr 22, 2012

ok so i should change the

LEFT(tk_job_desc, 150) as truncated_job_desc FROM in the sql statement and use the example you used to truncate the text then specify the length

echo substr( $title, 0, strrpos( substr( $title, 0, 35), ' ' ) );

<?php

echo substr $row_Recordset1['tk_job_desc'], 0 , strrpos( $row_Recordset1['tk_job_desc'], 0, 80), ' ' ) );

?>

would that work?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2012 Apr 25, 2012

Yes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 25, 2012 Apr 25, 2012

so can i change the sql statement from

LEFT(tk_job_desc, 150) as truncated_job_desc FROM

to

job_desc FROM

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2012 Apr 25, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 25, 2012 Apr 25, 2012

i have tried using <?php echo substr $row_Recordset1['tk_job_desc'], 0 , strrpos( $row_Recordset1['tk_job_desc'], 0, 80), ' ' ) ); ?>

and it is showing a sytnax error in dreamweaver

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 25, 2012 Apr 25, 2012

i looked again and added two more brackets

<?php echo substr ( $row_Recordset1['tk_job_desc'], 0 , strrpos( substr( $row_Recordset1['tk_job_desc'], 0, 80), ' ' ) ); ?>

but the results didnt display anything

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2012 Apr 25, 2012

Hi,

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 26, 2012 Apr 26, 2012

the sql statement is currently

mysql_select_db($database_hostprop, $hostprop);

$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);

the output

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

the changed version is

mysql_select_db($database_hostprop, $hostprop);

$query_Recordset1 = "SELECT tk_job_title, tk_job_location, tk_job_salary, 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);

<?php echo substr ( $row_Recordset1['tk_job_desc'], 0 , strrpos( substr( $row_Recordset1['tk_job_desc'], 0, 80), ' ' ) ); ?>

if you need anymore code let me know

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 26, 2012 Apr 26, 2012

Can you upload your site and post a link here?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 26, 2012 Apr 26, 2012

ok here is a link to the page its in the Latest job section (ignore the rest of the formatting)

http://www.thinkfuturerecruitment.co.uk/beta/what-we-do.php

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2012 Apr 27, 2012

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

the table name isjob_desc

is that what you mean?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2012 Apr 27, 2012

sorry how would i do that then? seems confusing

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2012 Apr 27, 2012

thanks getting there....i inserted the above code in the top of the php code and amended the correct fields..however its now just showing all the text with no truncating

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

Post your current code here

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2012 Apr 27, 2012

php

<?php require_once('Connections/hostprop.php'); ?>

<?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>";

?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

?>

<?php

// *** Validate request to login to this site.

if (!isset($_SESSION)) {

  session_start();

}

$loginFormAction = $_SERVER['PHP_SELF'];

if (isset($_GET['accesscheck'])) {

  $_SESSION['PrevUrl'] = $_GET['accesscheck'];

}

if (isset($_POST['userid'])) {

  $loginUsername=$_POST['userid'];

  $password=$_POST['password'];

  $MM_fldUserAuthorization = "";

  $MM_redirectLoginSuccess = "signup/signup-complete.php";

  $MM_redirectLoginFailed = "failed.php";

  $MM_redirecttoReferrer = false;

  mysql_select_db($database_hostprop, $hostprop);

 

  $LoginRS__query=sprintf("SELECT userid, password FROM think_signup WHERE userid=%s AND password=%s",

    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

  

  $LoginRS = mysql_query($LoginRS__query, $hostprop) or die(mysql_error());

  $loginFoundUser = mysql_num_rows($LoginRS);

  if ($loginFoundUser) {

     $loginStrGroup = "";

   

          if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}

    //declare two session variables and assign them

    $_SESSION['MM_Username'] = $loginUsername;

    $_SESSION['MM_UserGroup'] = $loginStrGroup;               

    if (isset($_SESSION['PrevUrl']) && false) {

      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];

    }

    header("Location: " . $MM_redirectLoginSuccess );

  }

  else {

    header("Location: ". $MM_redirectLoginFailed );

  }

}

?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

$maxRows_Recordset1 = 9;

$pageNum_Recordset1 = 0;

if (isset($_GET['pageNum_Recordset1'])) {

  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];

}

$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_hostprop, $hostprop);

$query_Recordset1 = "SELECT tk_job_title, tk_job_location, tk_job_salary, tk_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);

if (isset($_GET['totalRows_Recordset1'])) {

  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];

} else {

  $all_Recordset1 = mysql_query($query_Recordset1);

  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);

}

$pageNum_Recordset1 = 0;

if (isset($_GET['pageNum_Recordset1'])) {

  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];

}

$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_hostprop, $hostprop);

$query_Recordset1 = "SELECT tk_job_title, tk_job_location, tk_job_salary, tk_job_desc FROM think_jobsearch ORDER BY tk_job_ID DESC";

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

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$maxRows_Recordset2 = 3;

$pageNum_Recordset2 = 0;

if (isset($_GET['pageNum_Recordset2'])) {

  $pageNum_Recordset2 = $_GET['pageNum_Recordset2'];

}

$startRow_Recordset2 = $pageNum_Recordset2 * $maxRows_Recordset2;

mysql_select_db($database_hostprop, $hostprop);

$query_Recordset2 = "SELECT ID, FirstName, Surname, PositionReq, SalaryReq, skills_offered, location, LEFT(CanAdminInfo, 100) as truncated_CanAdminInfo FROM think_signup ORDER BY ID DESC";

$query_limit_Recordset2 = sprintf("%s LIMIT %d, %d", $query_Recordset2, $startRow_Recordset2, $maxRows_Recordset2);

$Recordset2 = mysql_query($query_limit_Recordset2, $hostprop) or die(mysql_error());

$row_Recordset2 = mysql_fetch_assoc($Recordset2);

if (isset($_GET['totalRows_Recordset2'])) {

  $totalRows_Recordset2 = $_GET['totalRows_Recordset2'];

} else {

  $all_Recordset2 = mysql_query($query_Recordset2);

  $totalRows_Recordset2 = mysql_num_rows($all_Recordset2);

}

$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;

mysql_select_db($database_hostprop, $hostprop);

$query_Recordset3 = "SELECT * FROM think_wwd";

$Recordset3 = mysql_query($query_Recordset3, $hostprop) or die(mysql_error());

$row_Recordset3 = mysql_fetch_assoc($Recordset3);

$totalRows_Recordset3 = mysql_num_rows($Recordset3);

?>

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2012 Apr 27, 2012

this is what i already have in the code which looks the same as above?

<?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>";

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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']; ?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 27, 2012 Apr 27, 2012

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

and replace with this

<?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>";

?>


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines