SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    Spain
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extracting the first 5 letters from a word.

    Top o' the morning to you.

    I am quite new to PHP, so please excuse my ignorance.
    I am trying to put a web site together and apparently it won't go without help from you, guys.

    Here is the source that works fine if you feed it with a sentance and it returns the first N words of the sentance.

    However, I would like to print the first N characters of a word.


    function characters($str) {

    global $numberofwords;

    $i=0;
    $offset = 0;
    while (($i < $numberofwords) and (strpos($str, ' ', $offset)>0)) {
    $offset = strpos($str, ' ', $offset);
    $offset += 1;
    $i++;

    }
    if ($i < $numberofwords) {
    return $str;
    }else {
    return substr($str, 0, $offset-1);
    }
    }


    What do I have to do, in order to print N number of letters from a word instead of N number of words from a sentence.

    Thank you for your time reading this post.

    Your Input is greatly appreciated.

    Regards,
    Alex.

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use substr()

    PHP Code:
    $str substr 'someword'0); 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Member
    Join Date
    May 2003
    Location
    Spain
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked! Thank you very much for the great reply.

    You have just made my day.

    Regards,
    hooli.

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome. *Cough*

    Sean
    Last edited by seanf; May 18, 2003 at 09:36.
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd better get that cough checked out
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite nasty isn't it

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


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
  •