Results 1 to 5 of 5

Thread: Create a php script to replace a string in multiple files

  1. #1
    Join Date
    Aug 2009
    Posts
    53

    Create a php script to replace a string in multiple files

    I wanted to know if anyone had an idea that could help me solve my problem.

    I have a .txt file that looks like XML tags...
    (AB: XXXXXXXX) (CD: YYYYYYYY bla bla bla)) ((AB: XXXXX) and so on ...

    I would like to create a php script that would work for each string starting with (AB: replace the 8 characters following (and therefore in the example XXXXXXXX) by 8 other characters, same with (CD: ...

    The snippet below works but only processes a single request. It is clear in this case, it positions the cursor and replaces the 8 characters following by uuuuuuuu. Only here in the file there are several BLOCK 1 and BLOCK 2. So logically it should loop?

    If anyone have an idea with foreach $content as $content that could do?

    PHP Code:
    $file "test.txt";
    $text=fopen($file,'r') or die("File missing" );
    $content=file_get_contents($file);
    // BLOCK 1 
    $findme_bloc1   '{AB:';
    $pos_bloc1 strpos($content$findme_bloc1);
    if (
    $pos_bloc1 !== false)
    {
    $pos_bloc1 $pos_bloc1 4;
    $contentMod substr_replace($content'uuuuuuuu'$pos_bloc18);
    }
    // BLOC 2 
    $findme_bloc2   '{CD:';
    $pos_bloc2 strpos($contentMod$findme_bloc2);
    if (
    $pos_bloc2 !== false)
    {
    $pos_bloc2 $pos_bloc2 4;
    $contentMod2 substr_replace($contentMod'uuuuuuuu'$pos_bloc28);
    }
    fclose($text);
    $text2=fopen($file,'w+') or die("File missing" );
    fwrite($text2,$contentMod);
    fclose($text2); 

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Create a php script to replace a string in multiple files

    Since you have no loop statement like for or while, it can not complete.

    You should make a function (as you have two "blocks" to find, you can call it twice), in pseudo-code that would (I admit to being too lazy to correct syntax ):

    function myfunc ($ block, $ stream) {

    I want to block stream from the starting position
    block as found in stream {
    I replaced the chain that goes into stream using the position found
    I move the starting position, taking into account the number of characters and replaced
    I want to block stream from the new starting position
    }
    return stream
    }

    The last line while serving to start your search from the last occurrence found.

  3. #3
    Join Date
    Aug 2009
    Posts
    53

    Re: Create a php script to replace a string in multiple files

    I tried that but it gives me error messages as shown below with the code that I tried:

    PHP Code:
    <?php
    $file 
    "test.txt";
    function 
    CheckBloc ($bloc$stream,$replace,$pos)
    {
    $pos_bloc 0;
    while (
    $pos_bloc strpos($stream,$bloc,$pos_bloc))
    {
     
    $pos_bloc $pos_bloc $pos;
     
    $stream substr($stream,$replace,$pos_bloc,8);
    }
    return 
    $stream;
    }
    $text=fopen($file,'r') or die("File missing" );
    //$content=file_get_contents($file); 
    $content file($file);
    $content_new CheckBloc('{AB',$content,'uuuuuuuu',6);
    $content_new CheckBloc('{CD:',$content_new,'uuuuuuuu',4);
    fclose($text);
    $text2=fopen($file,'w+') or die("File missing" );
    fwrite($text2,$content_new);
    fclose($text2);
    ?>
    Warning: strpos() expects parameter 1 to be string, array given in D:\PRIV\EasyPHP5.3.0\www\test.php on line 7
    Warning: strpos() expects parameter 1 to be string, array given in D:\PRIV\EasyPHP5.3.0\www\test.php on line 7
    Warning: fwrite() expects parameter 2 to be string, array given in D:\PRIV\EasyPHP5.3.0\www\test.php on line 25

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Create a php script to replace a string in multiple files

    It sounds like json, is it a base? Try regexp once and see if that works.

    And about your code, a print_r($stream) should also help ..

  5. #5
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Create a php script to replace a string in multiple files

    I think the problem lies into ''xxxxx' and 'yyyyy'

    PHP Code:
    $str "{AB:XXXXXXXX}{CD:YYYYYYYY}{blablabla}{AB:XXXXX}";
        
    $pat[] = '/(\{AB:)(\w+)(\})/';
    $pat[] = '/(\{CD:)(\w+)(\})/';
        
    $rep[] = '$1aaaaa$3';
    $rep[] = '$1bbbbb$3';
        
    echo 
    preg_replace($pat$rep$str);
     
    // {AB:aaaaa}{CD:bbbbb}{blablabla}{AB:aaaaa} 

Similar Threads

  1. Replies: 10
    Last Post: 22-10-2011, 10:21 PM
  2. Replace backslash in a string
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 05:43 AM
  3. Anyone have Script to edit multiple files?
    By Joyjeet in forum Software Development
    Replies: 3
    Last Post: 19-11-2009, 08:21 AM
  4. Replace a string in multiple files Linux
    By ADISH in forum Tips & Tweaks
    Replies: 0
    Last Post: 27-06-2009, 10:07 AM
  5. Script to create multiple groups
    By Saphire in forum Active Directory
    Replies: 1
    Last Post: 18-08-2008, 11:51 PM

Tags for this Thread

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
  •  
Page generated in 1,643,021,793.29045 seconds with 17 queries