How to echo 4 word from a sentence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon2x1
    New Member
    • Dec 2008
    • 123

    #1

    How to echo 4 word from a sentence

    I want to echo only 4 word from the sentence i selected from the database example (i am the only at home) if i select it from my database and i echo it i want (i am the only)
    to display.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    try to understand explode function

    Comment

    • simon2x1
      New Member
      • Dec 2008
      • 123

      #3
      Code:
      <?
      $word = "the man is on the way to is house";
      
      echo "the man is on the way to is house";
      
      //the result is these below
      //the man is on the way to is house
      ?>
      if i echo the string $word it will display this "the man is on the way to is house" i just want "the man is on" to display alone how can i do these

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        Originally posted by simon2x1
        Code:
        <?
        $word = "the man is on the way to is house";
        
        echo "the man is on the way to is house";
        
        //the result is these below
        //the man is on the way to is house
        ?>
        if i echo the string $word it will display this "the man is on the way to is house" i just want "the man is on" to display alone how can i do these
        What do you need this for?
        From your explanation of the problem I would say that solution is:

        Code:
        $word = "the man is on the way to is house";
        echo implode(" ", array_slice(explode(" ", $word), 0, 4));
        array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys = false ]] )

        string implode ( string $glue , array $pieces )

        array explode ( string $delimiter , string $string [, int $limit ] )

        Comment

        Working...