SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Explode vars from text file???

    Hello,

    Is it possible to extract different variables from a flat textfile by using PHP-explode() function??

    For instance my text file looks like:

    var1=blah&var2=blahblah&var3=.....

    Now i want to extract the variable names (for cms-issues)

    PHP Code:
    $data explode("&"$val); 
    But this will only extract & and not var1=, var2= etc.

    After this I want to create a simple update-php-snippet.


    Any suggestions

    Thx

    Btw: i don't have a database

  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)
    Try this to see what you get:

    PHP Code:
        $vars explode"&"$string);

        foreach (
    $vars as $variable) {

            echo 
    $variable;
            echo 
    "<br>";

        } 
    Sean
    Harry Potter

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

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to actually use the vars you can use.

    PHP Code:
    $str 'var1=blah&var2=blahblah&var3=foo';
    $tmp explode("&"$str);
    foreach(
    $tmp as $key => $val) {
        
    $ttmp explode("="$val);
        foreach(
    $ttmp as $key => $val) {
            ${
    $ttmp[0]} = $ttmp[1];
            }
        }
    print 
    "$var1<br>";
    print 
    "$var2<br>";
    print 
    "$var3<br>"
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx you guys (+girl??)

    It work no surprise for u there(fdPHP)
    Freddy could u, in brief tell me what exactly happens with:
    foreach($ttmp as $key => $val) {
    ${$ttmp[0]} = $ttmp[1];
    }
    For those who are interested in the code:
    PHP Code:
    $file file("text.txt"); 
    while(list(
    $key,$val) = each ($file)) {
     
    $tmp explode("&"$val); 



    foreach(
    $tmp as $key => $val) {
        
    $ttmp explode("="$val);
        foreach(
    $ttmp as $key => $val) {
            ${
    $ttmp[0]} = $ttmp[1];
            }
        }
    print 
    "$pollGenre<br>";
    print 
    "$pollTitle<br>";
    print 
    "$pollQuestion<br>";
    print 
    "$a1<br>";
    print 
    "$a2<br>";
    print 
    "$a3<br>";
    print 
    "$a4<br>";
    print 
    "$r1<br>";
    print 
    "$r2<br>";
    print 
    "$r3<br>";
    print 
    "$r4<br>"
    Greetz,
    D-flyer

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by D-flyer
    thx you guys (+girl??)
    Nope, just a picture!

    Sean
    Harry Potter

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

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /*
    This first foreach() loops through each element of the "&" exploded array
    */
    foreach($tmp as $key => $val) {
    /*
    This explodes on the = for each element
    */
        
    $ttmp explode("="$val);
    /*
    this loops through the second array
    */
        
    foreach($ttmp as $key => $val) {
    /*
    So at this point we have two elements $ttmp[0] and 
    $ttmp[1]. $ttmp[0] holds that which was on the right side of the equal sign and the $ttmp[1] on the left 
    side of the = sign.

    ${$var} means evaluate the var name and make a new var 
    from the name with the value of $ttmp[1]. So if $ttmp
    [0] was var1 after that line runs a new var named var1
     is created holding the value from the other side of the = sign. In other words $var1 now = whatever.

    */
            
    ${$ttmp[0]} = $ttmp[1];
            }
        } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you never know

    btw: nice picture collection you got, but it ain't Buffy 'the vampire slayer'

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope I will stick with Nina here and have Sarah for the site when it is up

    Sean
    Harry Potter

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

  9. #9
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FreddydoesPHP

    TheBossofFreddydoesPHP does a great job, thanks for the explanation (have to read a couple of times to let it sink in).
    Are you a teacher??

  10. #10
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy is a "dumb PHP codin' cat"

    Sean
    Harry Potter

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

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I am no teacher, so be sure to double-check after I give any code. My mom told me 50% of what comes out of my mouth is complete BS.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •