SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace data in file!?

    Is there a fast and easy way to replace data in a file?
    I have a big file with data like this in it

    34344
    65857
    54646
    46464
    ..

    I would like to replace the first number or first 2 numbers with the word "name"

    Thanx
    - m0h

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this will replace 2 digits with "name". i assume the numbers that you want to replace are at the beginning of each line? cuz that's what this will work on:

    PHP Code:
    $handle fopen('file.txt''r+');
    $contents '';

    while (!
    feof($handle)) { $contents .= fgets($handle10240); }

    $contents preg_replace('/^\\d{2}/m''name'$contents);

    fputs($handle$contents);
    fclose($handle); 
    i hope that works. i don't really know much about opening/reading files in PHP, but that preg_replace() is what you want. if you only want to replace 1 number on each line, just take out the "{2}" part.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx its working fine!
    - m0h


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
  •