Break down a url string into variables?

Discussion in 'PHP' started by MrLeN, Mar 10, 2011.

  1. #1
    I have created this form:

    
    <form action="/import.php" method="post">
    <p><input type="text" name="site_import" id="site_import" value="" /><input type="submit" value="Import" /></p>
    </form>
    
    Code (markup):
    Now, if I enter a url into the box:

    
    http://www.awebsite.com/?apples=1&oranges=2&pears=3
    
    Code (markup):
    I want the form to make a file that has this in it:

    
    <?php
    $apples = "1";
    $oranges = "2";
    $pears = "3"
    ?>
    
    Code (markup):
    How can I do this?
     
    MrLeN, Mar 10, 2011 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    This is what I have so far, but it's not working out to good:

    
    <?php
    $pieces = explode('&', $_POST['site_import']);
      foreach ($pieces as $p) {
        list($name, $value) = explode('=', $p);
        $_POST['$name'] = $value;
    		echo "\$".$name."=".$value."\n\n";
      }
    ?>
    
    Code (markup):
     
    MrLeN, Mar 10, 2011 IP
  3. Client Management System

    Client Management System Peon

    Messages:
    19
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $urlParts = explode('&', $_SERVER['QUERY_STRING']);
    
    foreach ($urlParts as $urlPart)
    {
        $parsedPart = explode('=', $urlPart);
    
        global $$parsedPart[0];
    
        $$parsedPart[0] = $parsedPart[1];
    }
    
    echo "Apples: $apples <br/>";
    
    echo "Oranges: $oranges <br/>";
    PHP:
    index.php?oranges=1&apples=3
    Code (markup):
    Not one of the cleanest ways of doing it but you should get the idea.
     
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    joebert, Mar 10, 2011 IP