appending characters to a list

Discussion in 'PHP' started by IamNed, Oct 26, 2010.

  1. #1
    i have a long list of numbers like this

    36346346
    436346
    444444
    44
    666666666666865
    4564
    .
    .
    .

    and at the beginning and end of each entry i want to append some characters . how do to this with php? thanks
     
    IamNed, Oct 26, 2010 IP
  2. KingOle

    KingOle Peon

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is your list in an array? If so, you can use a foreach statement to do this. E.g.

    
    $array = array(1234,12345,123456,1234567);
    foreach($array as $value) {
    $string .= $value . "SOMECHARACTERS";
    }
    
    echo $string;
    
    PHP:
     
    KingOle, Oct 26, 2010 IP
  3. IamNed

    IamNed Peon

    Messages:
    2,707
    Likes Received:
    276
    Best Answers:
    0
    Trophy Points:
    0
    #3
    lol if I had the time to add a comma at the end of each entry why would I need this code in the first place?

    anyone else got ideas?
     
    IamNed, Oct 26, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    You can remove <br /> its just to show how it would look

    
    <?php
    
    $str = "
    36346346
    436346
    444444
    44
    666666666666865
    4564";
    $text = str_replace("\n", "<br />\n^^^^^", $str); // ^^^^^ = your characters or are they different on each line ?
    
    echo $text;
    ?>
    
    PHP:
    Example out put:
    
    ^^^^^36346346
    ^^^^^436346
    ^^^^^444444
    ^^^^^44
    ^^^^^666666666666865
    ^^^^^4564
    
    Code (markup):
     
    Last edited: Oct 26, 2010
    MyVodaFone, Oct 26, 2010 IP
  5. KingOle

    KingOle Peon

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So what is your list then? A text file of numbers one after the other with a linebreak separating them?

    
    $filename = "c:\\folder\\number.txt";
    
    $file = fopen($filename, "w+"); 
    
    if(!$file) return false;
    
    $data = file_get_contents($filename);
    fclose($file);
    
    $data = explode("\n", $data);
    
    foreach($data as $value) {
        $string .= $value . "SOMECHARACTERS";
    }
    
    echo $string;
    
    PHP:
    You may have to try other characters for the explode function depending on the newline value. <br />, \nr, \r or w/e else represents a newline.
     
    KingOle, Oct 26, 2010 IP
  6. IamNed

    IamNed Peon

    Messages:
    2,707
    Likes Received:
    276
    Best Answers:
    0
    Trophy Points:
    0
    #6
    that looks good. problem solved
     
    Last edited: Oct 26, 2010
    IamNed, Oct 26, 2010 IP