I'm using preg_replace to try and replace all instances of a certain subject in one file with strings from another file. (It's basically a translation system for installing stuff.)
Here's file A:
BEGIN ~D0QPPOTN~ 1
IF ~Global("PotionJob","Global",0)~ THEN BEGIN 0
SAY @0
IF ~~ THEN REPLY @1 GOTO 1
IF ~~ THEN REPLY @2 GOTO 2
IF ~~ THEN REPLY @3 GOTO 3
IF ~~ THEN REPLY @4 GOTO 4
IF ~~ THEN REPLY @5 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 5
IF ~~ THEN REPLY @6 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 6
IF ~~ THEN REPLY @7 GOTO 7
END
IF ~~ THEN BEGIN 1 // From 0.0
SAY @8
IF ~~ THEN REPLY @9 GOTO 2
IF ~~ THEN REPLY @3 GOTO 3
IF ~~ THEN REPLY @4 GOTO 4
IF ~~ THEN REPLY @5 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 5
IF ~~ THEN REPLY @6 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 6
IF ~~ THEN REPLY @7 GOTO 7
END
IF ~~ THEN BEGIN 2 // From 0.1, 1.0
SAY @10
IF ~~ THEN REPLY @1 GOTO 1
IF ~~ THEN REPLY @3 GOTO 3
IF ~~ THEN REPLY @4 GOTO 4
IF ~~ THEN REPLY @5 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 5
IF ~~ THEN REPLY @6 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 6
IF ~~ THEN REPLY @7 GOTO 7
END
IF ~~ THEN BEGIN 3 // From 0.2, 1.1, 2.1
SAY @11
IF ~~ THEN REPLY @12 GOTO 1
IF ~~ THEN REPLY @13 GOTO 2
IF ~~ THEN REPLY @4 GOTO 4
IF ~~ THEN REPLY @5 DO ~SetGlobal("PotionRoger","Global",1)~ GOTO 5
IF ~~ THEN REPLY @6 DO ~SetGlobal("PotionRoger","Global",1)~GOTO 6
IF ~~ THEN REPLY @7 GOTO 7
END
And here's file B:
@0 = ~Greetings, so few people come to visit Jadarath in his laboratory. Now, feel free to chat, but please don't try to stymie my work, or I'll have Roger call in a contact and your friends will find your arrow-filled body lying in an alleyway. ~ [POTION]
@1 = ~Whoah, a little hostile? I've done nothing to you.~
@2 = ~Laboratory? I see no such thing. Why are you down here in this barren cesspit?~
@3 = ~Are you some sort of mage? Could you craft potions for me?~
@4 = ~What work could you be doing down here?~
@5 = ~What business do you have with Roger?~
@6 = ~Who is Roger?~
@7 = ~I'll be taking my leave now.~
@8 = ~But you might. Anyone might. I can't have anyone disrupting the work. Just don't interfere with what I'm upto and we'll be friends. Good friends. Best friends.~
@9 = ~Anyway, I don't see a laboratory. What are you doing down here~
@10 = ~Rent is low and the good restaurants are just a ladder away. Why is anyone down here! I'm busy. I don't want people disrupting my work, which incidentally does not require aany laboratory more complicated than the human mind and body, and too many people topside keep trying to do so. But let me tell you, it's pretty crowded, what with the band of rogues, Shadow Thief storekeeper, evil wizard, paladin, strange cult of eyeless fanatics, rakshasa leading a band of kobolds, and sundry monsters. Not to mention random adventurers. So don't get snippy with me. ~
@11 = ~I'm no sage. Merely a humble, herbalist who knows his way around a few minor magics. All my excess potions are sold to Roger, who maintains an excellent inventory. Why not buy from him?~
@12 = ~Weren't you a little hostile before? I'd done nothing to you.~
@13 = ~I see no laboratory down here. Why are you down here in this barren cesspit?~
So, all I'm doing is grabbing the stuff in file B (the numbers and the strings) and trying to replace the numbers that match in file A with the strings from file B. (The stuff between the tildes.)
Here's my script:
<?php
$file = "fileb";
$handle = fopen($file,"r+b");
$string = fread($handle,filesize($file));
$tra_array = array();
$text_array = array();
preg_match_all("#@(.*)\s{1,}= (~.*~)#sU",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++){
$tra_array[] = $matches[1][$i];
$text_array[] = $matches[2][$i];
}
fclose($handle);
$file = "filea";
$handle = fopen($file,"r+b");
$string = fread($handle,filesize($file));
for($i = 0; $i < count($tra_array); $i++) {
$subject = "#@".$tra_array[$i]."#s";
$replace = $text_array[$i];
$string = preg_replace($subject,$text_array[$i],$string);
}
echo "<pre>".$string."</pre>";
?>
When I echo out the contents of filea after using preg_replace, it works on the numbers from 0 - 9, but as soon as I get into double-digit numbers, it cuts off the last digit. (i.e. It's replacing @10 with the string from @1.)
I've echoed out $subject in the for loop and $subject comes out correctly ("#@10#s" for @10 for instance), but for some strange reason it won't replace @10 (or any double-digit entry) with the appropriate text from the $text_array.
Any thoughts?