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_bloc1, 8);
}
// 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_bloc2, 8);
}
fclose($text);
$text2=fopen($file,'w+') or die("File missing" );
fwrite($text2,$contentMod);
fclose($text2);
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.
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);
?>
Quote:
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
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 ..
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}