Jump to content


Photo

help with a preg-replace routine


  • Please log in to reply
2 replies to this topic

#1 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 02 May 2006 - 10:36 PM

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:


$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 values
var_dump($newdata);


TIA
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 02 May 2006 - 11:01 PM

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:
<?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"
?>


#3 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 02 May 2006 - 11:07 PM

[!--quoteo(post=370731:date=May 2 2006, 07:01 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ May 2 2006, 07:01 PM) View Post[/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:
<?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"
?>
[/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
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users