how to join 2 lines

Discussion in 'PHP' started by lavangiriece, Sep 4, 2013.

  1. #1
    Input
    ====
    email1
    name1
    email2
    name2
    email3
    name3

    output
    ====
    email1 | name1
    email2 | name2
    email3 | name3

    anyone help me please?
     
    lavangiriece, Sep 4, 2013 IP
  2. UtsavT

    UtsavT Active Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    50
    #2
    when you get those variables from a form

    <?php
    $email1 = $_POST['email1'];
    $name1 = $_POST['name1'];
    $email2 = $_POST['email2'];
    $name2 = $_POST['name3'];
    $email3 = $_POST['email3'];
    $name3 = $_POST['name3'];
    
    $final_line_1 = $email1 . " | " . $name1;
    $final_line_2 = $email2 . " | " . $name2;
    $final_line_3 = $email3 . " | " . $name3;
    ?>
    PHP:
    Hope this example above will help you to understand how to do that
     
    UtsavT, Sep 4, 2013 IP
    silvero likes this.
  3. cLogik

    cLogik Active Member

    Messages:
    159
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    90
    #3
    @UtsavT Defining every variable is unnecessary.

    If your source is a regular form, then you can on submit get the info by using the $_POST variable without defining each of them. For an example

    echo $_POST['email1'].' seperator '.$_POST['name1'];
    echo $_POST['email2'].' seperator '.$_POST['name2'];
    PHP:
     
    cLogik, Sep 5, 2013 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Maybe this:

    
    <?php
    $input = 'email1
    name1
    email2
    name2
    email3
    name3';
    $output = '';
    $temp = explode("\n", $input);
    $count = count($temp);
    for ($x = 0;  $x < $count; $x=$x+2) {
      $output .= $temp[$x] . ' | ' . $temp[$x+1] . "\n";
    }
    unset($temp);
    echo $output;
    
    PHP:
     
    ThePHPMaster, Sep 6, 2013 IP
  5. xxxize

    xxxize Member

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    38
    #5
    The other way is:

    <?php
      $users = array();
    
      $users[] = array("email" => $_GET['email1'], "name" => $_GET['name1']);
      $users[] = array("email" => $_GET['email2'], "name" => $_GET['name2']);
      $users[] = array("email" => $_GET['email3'], "name" => $_GET['name3']);
    
      foreach($users as $user) {
        $newline = $user['name'] . " | " . $user['email'];
        echo $newline;
      }
    ?>
    PHP:
    If it is not working check $_GET.. maybe needs $_POST.
     
    xxxize, Sep 8, 2013 IP
  6. Mackos

    Mackos Well-Known Member

    Messages:
    364
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #6
    Lol, guys it should be just:

    $e = explode('\n', $input);
    $output = implode(' | ', $e);
    Code (markup):
     
    Mackos, Sep 9, 2013 IP
  7. xxxize

    xxxize Member

    Messages:
    33
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    38
    #7
    yes but he need to manage the emails and names with some way. Where he found the $input array? somehow must create the array.. what better from key-value arrays..
     
    xxxize, Sep 9, 2013 IP
  8. humtuma

    humtuma Notable Member

    Messages:
    1,225
    Likes Received:
    24
    Best Answers:
    3
    Trophy Points:
    250
    #8
    Use the dot (.) operator to join two lines.
     
    humtuma, Sep 16, 2013 IP