0

I currently have this function to search and replace in a text file.

// Input
$ect = array('Visssa', 'Lisssa', 'her');

// Placeholders in the file
$put =  array('lname', 'fname', 'hisher');


// Replace the placeholders
$oput = str_replace($put, $ct, 'tmpfile.txt');

This is not the full program but the idea is replace the values found in tmpfile.txt with the ones found in the $etc array. And it works flawlessly.

But, what I need to do is get all passed vars (get/post) and then make the arrays so that the var is the value to replace and the value is the value to replace it with.

So, if I sent the url http://xyz.com/?lname=tom&ogre=yes

All instances of lname in the file would be replaces with tom and all instances of ogre would be replaced with yes.

So somehow it just gets any/all variables passed in get/post and then the arrays showed above would cause the var to be replaced by the value in the file.

1
  • foreach($_REQUEST as $key => $value) { echo 'Key = ' . $key . '<br />'; echo 'Value= ' . $value; } Commented Oct 26, 2012 at 4:43

2 Answers 2

1

Do this:

    $etc = array_keys($_GET);
    array_walk($etc,"addBraces");
    $put = array_values($_GET);
    $oput = str_replace($etc, $put, 'tmpfile.txt'); 

    function addBraces(&$item)
    {
        $item = "{".$item."}";
    }

And of course, all the regular "always santize/escape your data" etc...

Sign up to request clarification or add additional context in comments.

4 Comments

This did not seem to replace the variables. Of course I tried with $_REQUEST and not $_GET.
Got this method to work now changed last line to: $oput = str_replace($etc, $put, 'tmpfile.txt');
But, same question as below - if I want to automatically enclose each var_key in {} to make it so it only replaces enclosed {values} in the text file... How? ?lname=hello will replace all instances of lname in the text file, but how can it automatically enclose it so it would replace all instances of {lname} with hello but leave an un{enclosed} lname alone?
@Chris: you need to add braces {} to the etc array and use it. I have updated the answer to reflect it
0

parse_str() will be perfect for this

function addreplacetokens(&$input)
{
  $input = '{' . $input . '}';
}

$string = 'word {lname} word {fname} word {hisher} word';
$filter = array('lname','fname','hisher');
parse_str($_SERVER['QUERY_STRING'],$replacements);
foreach($replacements as $key => $value)
  if(in_array($key,$filter) == false)
    unset($replacements[$key]);
// this next block can be removed if you don't want a default
foreach($filter as $key)
  if(array_key_exists($key,$replacements) == false)
    $replacements[$key] = ''; // change this!
// copy the array so the keys can be filtered
$replacements_keys = array_keys($replacements);
array_walk($replacements_keys,'addreplacetokens');
$string = str_replace($replacements_keys,$replacements,$string);
echo $string;

so that ?lname=hello will output

word hello word word word

the next issue, is do you need to deal with word boundaries or not?

4 Comments

What you might want to do is filter $replacements on your placeholder values though after it's been parsed from the Query string to isolate only the replacements you want.
see my revised code, it has an array of your filters, so that other query string parameters can be ignored.
The first method worked, I understand the filter so that only the wanted text is replaced... Is there a way to instead make it simply look for a "mark" of sorts. so ?lname=hello doesn't replace all instances of lname but of {lname} for example. This way, only actual replaceable variables marked with {variable} would be replaced so if someone put ?the=cat all the instances of the are not replaced unless in the textfile it is marked {the}? The idea is not to have to edit the php file ever but only the text files. So the filter method will not achieve that but a "mark" would.
Reveiw my edit, I've changed it to a token system where "{lname}" is the token instead of just "lname".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.