phporcaffeine Posted May 2, 2006 Share Posted May 2, 2006 Let me explain -I have an .rtf (rich text file) that is a "template" for a report format. My script gets values n' such from a DB then it open/reads the template file.In the template file, there are " keywords " or " flags " that mark where the different values go, I then replace the individual flag with the appropriate value and fwrite the new file data to a seperate .rtf file.It's very doable but I am running into a logic issue. I can't figure out how to replace a given "flag" with the appropriate value foreach() flag that preg_replace comes across.Example:[code]$flags = array("flaga", "flagb", "flagc", "flagd");$values = array("flaga" => $valuea, "flagb" => $valueb, "flagc" => $valuec, "flagd" => $valued);$filedata = file("../my_template.rtf");foreach ($filedata as $key +> $value) {$hook = preg_replace($flags, (how do I get the appropriate value here for whatever preg_replace found?), $value);$newdata .= $hook . "\n";}//$newdata should now contain the whol .rtf file with all the flag characters replaced with their appropriate valuesvar_dump($newdata);[/code]TIA Quote Link to comment Share on other sites More sharing options...
zq29 Posted May 2, 2006 Share Posted May 2, 2006 If you're not using any advanced rules for your replacements it would be better to use str_replace() instead of preg_replace(). In which case, you can use it like this:[code]<?php$search = array("old1","old2","old3");$replace = array("new1","new2","new3");$str = "old1 old2 old3";echo str_replace($search,$replace,$str);//The above will echo "new1 new2 new3"?>[/code] Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted May 2, 2006 Author Share Posted May 2, 2006 [!--quoteo(post=370731:date=May 2 2006, 07:01 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ May 2 2006, 07:01 PM) [snapback]370731[/snapback][/div][div class=\'quotemain\'][!--quotec--]If you're not using any advanced rules for your replacements it would be better to use str_replace() instead of preg_replace(). In which case, you can use it like this:[code]<?php$search = array("old1","old2","old3");$replace = array("new1","new2","new3");$str = "old1 old2 old3";echo str_replace($search,$replace,$str);//The above will echo "new1 new2 new3"?>[/code][/quote]No, no advanced rules here -I see what your doing though, as long as the value stays in the same stack position as the "flag" then it will always match...I also know what you mean about preg_replace Vs. str_replace cause preg_ can be a stinker if your search string has anything but straight alpha in it.-damint Jim, why didn't I think of that!-Thanks bro Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.