Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy PHP Limiting Text Characters

    Hello,

    I am new here and new to PHP coding. My question is, how do I change this function below from 30 Characters to 20 Characters??? Just change the number 30 to 20??

    <?
    $str_towrite=$rst["product_name"];

    if (strlen ($str_towrite)>30)
    {
    echo substr($str_towrite, 0, strrpos( substr($str_towrite, 0, 30),' ' )) . "...";
    }
    else
    {
    echo $str_towrite;
    }
    ?>

    Can someone help me with this function...Please help. Would appreciate...

    Kind regards,

    Idunno

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welcome here !

    Indeed, just change the 30 to 20 should do it.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Hello raf,

    Wow! Thanks for the quick responds! I have tryed to change to 20 charecters, and when I upload script, only the "..." in the function will show at the end in the text, and not the wording/text???

    Kind regards,

    Idunno

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you post your edited code?
    + also the value you tested it with.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply!

    Here is a sample pic (attached).

    Thanks!

    Idunno
    Attached Thumbnails Attached Thumbnails PHP Limiting Text Characters-sample.gif  

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i see.

    now, this is not a codingerror. What the code does is

    echo substr($str_towrite, 0, strrpos( substr($str_towrite, 0, 20),' ' )) . "...";

    so it prints a substring (part of your string), starting from 0 until the last position of a blank, before the 20° character, and then add '...'.
    That's the
    strrpos( substr($str_towrite, 0, 20),' ' ) part
    (start from position 20 and then go back to the start of the string until the first blank)

    But your value doesn't contain a blank, so the substr will go from 0 to 0

    This function (it's taken from a usercomment i the online manual ... (yes we know )) is used to cut on a whole word, but maybe you don't need that, or maybe you shouldn't need to use that, given the type of values that you have ...

    If you just need the first 20 characters, then all you need is

    echo substr($str_towrite, 0, 20) . ' ...';
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Hi Raf,

    I did what you said: echo substr($str_towrite, 0, 20) . ' ...'; and it works fine now!!!!!

    Thanks a million guy!! Couldn't do it with out you!!

    I am glad their are still people out their like your self that don't mine helping folks like myself. I am glad you were there!!!

    Kindest & Warmest Regards,

    Idunno

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're welcome !

    Glad i could help Happy coding!
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •