Jump to content

help with a preg-replace routine


Recommended Posts

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 values
var_dump($newdata);
[/code]


TIA
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[!--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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.