Explode function simple question

Discussion in 'PHP' started by bloard, May 13, 2006.

  1. #1
    VERY new to PHP, or any programming language for that matter. So far I'm gettig by thanks to google search and my reference book for help. This one I'm sure is very simple, but can't seem to find anything...

    Im using the "explode" function but need to put a variable in the string separator immediately following some text. Don't know how to do this...

    $var = some number
    $array = explode('sometext$var',$string);


    meaning, my stringseparator varries while in a loop depending on the value of $var, but is appended to some various text. So lets say $var is equal to 20 in this loop. then the string separator would be "sometext20", but next loop it would be "sometext21"

    what is the proper format of the string separator?

    Thanks
     
    bloard, May 13, 2006 IP
  2. frisby

    frisby Well-Known Member

    Messages:
    1,378
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Heh, what you want.

    If you want explode word "something" where is letter 'e', you would do this explode('e',"something"); and the result will be 2 values in array, "som" and "thing".

    Clear now? :rolleyes:
     
    frisby, May 13, 2006 IP
  3. bloard

    bloard Peon

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No.. I guess I'm not clear....

    I know how explode works, but this time I am using it while in a loop, where the string separator will change each time through the loop.

    lets say I had this data...


    First Time through the loop when $var=20
    $x = 3020t30t203030

    Second time through the loop when $var=19
    $x - 3020t30t193030

    So the value after the second "t" changes each time... and this is exactly where I want my string separator. So, I want the number following the second "t" in the string to be a variable b/c it can change each time through

    $array = explode ('t$var',$x);

    Follow me now? What is the appropriate syntax in the string separator?

    Thanks
     
    bloard, May 13, 2006 IP
  4. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you use double quotes " PHP will output the contents/value of $var instead of the string '$var'

    $array = explode ("t$var",$x);

    You can also use:

    $array = explode ('t'.$var,$x);

    The double quotes works for every function and on echo, etc...


    
    
    <?php
    
    $name = 'themole';
    
    echo 'Hello $name!'; //This outputs Hello $name!
    
    echo "Hello $name!"; //This outputs Hello themole!
    
    echo 'Hello '.$name.'!'; //This also outputs Hello themole!
    ?>
    
    
    Code (markup):
    -the mole
     
    themole, May 13, 2006 IP
  5. bloard

    bloard Peon

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks... like I said... my question was simpler and more novice than I was making it sound.
     
    bloard, May 13, 2006 IP
  6. frisby

    frisby Well-Known Member

    Messages:
    1,378
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    140
    #6
    I even didn't know, this was your problem, because your first line looks like pseudocode, so I predict the second is also.
     
    frisby, May 13, 2006 IP
  7. bloard

    bloard Peon

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry frisby... I really don't yet know the protocol of posting code questions that are understandable. I'll get there as I'm sure I'll have more questions.

    Thanks for the quick replies.
     
    bloard, May 13, 2006 IP