Limit length of echo call

Discussion in 'HTML' started by samir.doshi@gmail.com, May 22, 2006.

  1. Guest

    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
     
    , May 22, 2006
    #1
    1. Advertising

  2. Martin Jay Guest

    In message <>,
    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
     
    Martin Jay, May 22, 2006
    #2
    1. Advertising

  3. 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
     
    Beauregard T. Shagnasty, May 22, 2006
    #3
  4. Toby Inkster Guest

    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
     
    Toby Inkster, May 22, 2006
    #4
  5. 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
     
    Jonathan N. Little, May 22, 2006
    #5
  6. Toby Inkster Guest

    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
     
    Toby Inkster, May 22, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leszek Dubiel
    Replies:
    3
    Views:
    9,815
  2. Rahul Aggarwal
    Replies:
    3
    Views:
    4,507
    Netsharp Ninja
    Apr 6, 2004
  3. vadim
    Replies:
    0
    Views:
    476
    vadim
    May 20, 2004
  4. Replies:
    1
    Views:
    1,085
    Victor Bazarov
    Jun 28, 2005
  5. noisenet
    Replies:
    0
    Views:
    399
    noisenet
    May 29, 2007
Loading...

Share This Page