Limit length of echo call

S

samir.doshi

Hello,

I have job descriptions saved to my database table and want to call
them to output them to a table but only want to output a portion of the
saved job description....like the first 200 letters.

How can I do that?

Thanks
 
M

Martin Jay

I have job descriptions saved to my database table and want to call
them to output them to a table but only want to output a portion of the
saved job description....like the first 200 letters.

How can I do that?

PHP?

Try:

echo substr($string, 0, 200);
 
B

Beauregard T. Shagnasty

I have job descriptions saved to my database table and want to call
them to output them to a table but only want to output a portion of
the saved job description....like the first 200 letters.

How can I do that?

Depends on your language. In PHP, it would be: substr()

echo substr($jobdesc, 0, 200);
 
T

Toby Inkster

samir.doshi said:
I have job descriptions saved to my database table and want to call
them to output them to a table but only want to output a portion of the
saved job description....like the first 200 letters.

As you use the term 'echo' I'm going to assume you use PHP. ('echo' also
exists in shell scripting and DOS batch files, though they are not
generally noted for their database connectivity. Probably some other
languages use an 'echo' command too.) In future please specify what
language you're using -- you are likely to get more/better answers that
way.

In PHP, there exists a function substr(). (Almost identical functions
exist in C, Perl, Java and Javascript -- and probably other languages too.)

The syntax of substr() is either:

substr(STRING, START, LENGTH)
substr(STRING, START)

this will find a sub-string of the given STRING, starting at character
START (the characters are numbered from 0). If the LENGTH is given, the
sub-string is limited to that many characters; if not, the sub-string
continues to the end of the given STRING.

Examples:

<?php

$example = 'abcdefghij';

echo substr($example, 3, 2);
// 'de'

echo substr($example, 7, 3);
// 'hij'

echo substr($example, 7, 100);
// 'hij'

echo substr($example, 6);
// 'ghij'

echo substr($example, 0, 3);
// 'abc'

?>

So to limit your job description to 200 characters, you want to use:

<?php
echo substr($jobdesc, 0, 200);
?>

You might also want to include an ellipsis (…) character after that:

<?php
echo substr($jobdesc, 0, 200) . '…';
?>
 
J

Jonathan N. Little

Toby said:
You might also want to include an ellipsis (…) character after that:

<?php
echo substr($jobdesc, 0, 200) . '…';
?>

or maybe:

<?php

if(strlen($jobdesc)>200){
echo substr($jobdesc, 0, 200) . '…';
}
else {
echo $jobdesc;
}

?>
 
T

Toby Inkster

Jonathan said:
<?php

if(strlen($jobdesc)>200){
echo substr($jobdesc, 0, 200) . '…';
}
else {
echo $jobdesc;
}

?>

Yep -- even better. Though I prefer to allow a little grace for pieces
of text that are only just over the limit. For example:

<?php
if (strlen($jobdesc)>220)
echo substr($jobdesc, 0, 200) . '…';
else
echo $jobdesc;
?>

And I suppose we should be escaping any special characters...

<?php
if (strlen($jobdesc)>220)
echo htmlentities(substr($jobdesc, 0, 200)) . '…';
else
echo htmlentities($jobdesc);
?>

Bonus points for not leaving cutting it so that there are no partial wor...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top