Go Back   Velocity Reviews > Newsgroups > HTML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

HTML - Limit length of echo call

 
Thread Tools Search this Thread
Old 05-22-2006, 05:25 AM   #1
Default Limit length of echo call


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



samir.doshi@gmail.com
  Reply With Quote
Old 05-22-2006, 05:50 AM   #2
Martin Jay
 
Posts: n/a
Default Re: Limit length of echo call

In message < om>,
writes

>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);
--
Martin Jay
  Reply With Quote
Old 05-22-2006, 06:01 AM   #3
Beauregard T. Shagnasty
 
Posts: n/a
Default Re: Limit length of echo call

wrote:

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

--
-bts
-Warning: I brake for lawn deer
  Reply With Quote
Old 05-22-2006, 07:49 AM   #4
Toby Inkster
 
Posts: n/a
Default Re: Limit length of echo call

samir.doshi wrote:

> 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) . '…';
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

  Reply With Quote
Old 05-22-2006, 02:34 PM   #5
Jonathan N. Little
 
Posts: n/a
Default Re: Limit length of echo call

Toby Inkster wrote:

> 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;
}

?>
--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
  Reply With Quote
Old 05-22-2006, 07:21 PM   #6
Toby Inkster
 
Posts: n/a
Default Re: Limit length of echo call

Jonathan N. Little wrote:

> <?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...

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump