Explode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The1corrupted
    New Member
    • Feb 2007
    • 134

    Explode

    I have a question about the explode function and am rather confused...
    Say $msg=I like Corn
    If I have [PHP]<?php
    $mesg=explode(" ", $msg, 3);
    echo $mesg[0].$mesg[2];
    ?>[/PHP]
    will I manage to be able to pull this result:
    "I like"?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What you are echoeing in your sample is "ICorn" because you used the first (msg[0]) and the third (msg[2]) entries without connecting blank character.

    In order to display "I like" your do[php]echo $mesg[0].' '.$mesg[1];[/php]

    Ronald :cool:

    Comment

    • The1corrupted
      New Member
      • Feb 2007
      • 134

      #3
      Ah... Also, can I double explode strings? Say...

      [PHP]
      <?php
      $mesg=explode(" ", $msg, 2);

      if ($mesg[0]=="word") {
      $mesg=explode(" ", $msg, 3);
      }

      ?>[/PHP]
      Or would I have to redefine $mesg in the if statement as another variable?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by The1corrupted
        Ah... Also, can I double explode strings? Say...

        [PHP]
        <?php
        $mesg=explode(" ", $msg, 2);

        if ($mesg[0]=="word") {
        $mesg=explode(" ", $msg, 3);
        }

        ?>[/PHP]
        Or would I have to redefine $mesg in the if statement as another variable?
        Use another variable for the array

        Code:
         <?php 
        $mesgArray1=explode(" ", $msg, 2);
         
        if ($mesgArray1[0]=="word") {
        $mesgArray2=explode(" ", $msg, 3);
        }
        ?>
        The value of $msg remains the same in this case

        Comment

        Working...