Results 1 to 6 of 6

Thread: print out characters

  1. #1
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default print out characters

    Building a script for email processing, and I am having problems limiting the number of characters given for a certain field. the function will take 2 arguments, the input, and some length. it returns the string to a maximum of "length" characters.

    PHP Code:
    function friendly($text$length=1024) {
     for(
    $i=0$i<$length$i++) {
       echo 
    ____character____
     
    }

    I only want to print out 1 character at a time, until the limit is reached then it would end


    so if i were to call

    Code:
    friendly("my name is josh", 5)
    it would return "my na"

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Why not try taking out this part (in red) in the code:

    Code:
    function friendly($text, $length=1024) {
     for($i=0; $i<$length; $i++) {
       echo ____character____
     }
    }
    Then call the script like you have posted above.
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    the 1024 is a default value, incase you dont pass anything, preventing the script from erroring, but if you pass a number into the argument it gets overwritten... what I am looking for help on is actually printing out the characters 1 at a time


    and yes i have tried without the value, still havent been able to get the function to print out 1 character at a time

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There's no need for this, the built-in substr() function will do just as well:
    Code:
    $tring = 'My name is Josh';
    echo substr($tring, 0, 5);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Twey is right, but for the original, I'd use the function strlen($str), where $str is the string being accessed, to go about the for-loop, rather than an arbitrary value which, in the best case, wastes time, or, for a long string, won't finish the string.


    EDIT: I just noticed your amusing string name. //laugh.
    Last edited by djr33; 04-10-2007 at 12:34 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    WoW, talk about a brain cramp. thanks twey

Bookmarks

Posting Permissions

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