Re: ACK! str_replace and arrays not working?

From: Paul Barfoot (Paul_at_theglobalfamily.fsworld.co.uk)
Date: 10/07/04


Date: Thu, 7 Oct 2004 11:08:52 +0100

Hi Joel

Here's my abridged and annotated version of what I think you're trying to
achieve i.e replace all occurrences of <j> in a file with the contents of
the test.txt file:

<?
//load text files into variables
$formFile = "main.txt";
$stJavaFile = "test.txt";

//suppress errors with @ and load text files into variables

/* I couldn't find fail in my version of PHP so I used die() instead. Also
file() doesn't need "r" - you're thinking of fopen */

if (!$form = @file($formFile)) {
die("Cannot open file: " . $formFile . "!");
}

/* if the search element of str_replace is a string as in your example then
the replacement element should also be a string, not an array. Use join() to
bring in the file as a string as below. If you make the search element into
an array using something like $search = array("<j>") then this is only
replaced by the first element of the replacement array i.e. the first line,
which is not what I think you want */

if (!$stJava = join( '', @file($stJavaFile))) {
die("Cannot open file: " . $stJavaFile . "!");
}

$newForm = str_replace("<j>",$stJava,$form);

foreach ($newForm as $line) {
echo "$line\n";
}
?>

-- 
Paul Barfoot
The Global Family Website is at: http://tgf.athnic.com
"Joel C Bennett" <JoelCBennett@gmail.com> wrote in message 
news:2cd7a0bc.0410061358.73f8f9c5@posting.google.com...
>I am attempting to replace in an array (read in from a text file)
> wherever a "<j>"  appears with the contents of a different array
> (filled from a different text file).  It is returning "Array".  What
> am I doing wrong?
>
> here is the code:
>
> //load text files into variables
> $formFile = "main.txt";
> $studInfoFile = "studinfo.txt";
> $rbJavaFile = "rbjava.txt";
> $stJavaFile = "test.txt";
> $rbFormFile = "rbform.txt";
> $testFile = "test.txt";
>
> //supress errors with @ and load text files into variables
> if (!$form = @file($formFile, 'r')) {
> fail("Cannot open file: " . $formFile . "!");
> }
> if (!$studInfo = @file($studInfoFile, 'r')) {
> fail("Cannot open file: " . $studInfoFile . "!");
> }
> if (!$rbjava = @file($rbJavaFile, 'r')) {
> fail("Cannot open file: " . $rbjavaFile . "!");
> }
> if (!$stJava = @file($stJavaFile, 'r')) {
> fail("Cannot open file: " . $stJavaFile . "!");
> }
> if (!$rbForm = @file($rbFormFile, 'r')) {
> fail("Cannot open file: " . $rbFormFile . "!");
> }
>
>
> $newForm = str_replace("<j>",$stJava,$form);
>
> foreach ($newForm as $line) {
> echo "$line\n";
> } 


Relevant Pages

  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: Regular expression optimization
    ... position in the replacement array of strings, ... > input string and a MatchEvaluator delegate. ... > The first part required combining the separate Regular Expression strings ...
    (microsoft.public.dotnet.general)
  • Re: Regular expression optimization
    ... string and a MatchEvaluator delegate. ... The first part required combining the separate Regular Expression strings ... The class has a private string array of replacement ...
    (microsoft.public.dotnet.general)
  • Re: Emptying an array of string
    ... I'm using String to fill the array. ... >>I'll either have to reinitialize the array or use char. ... An empty String is a String object with no characters in it. ... I was wrong about the replacement of Strings. ...
    (comp.lang.java.help)
  • Re: FAQ 4.53 How do I manipulate arrays of bits?
    ... Thanks for noticing it. ... Here's my replacement: ... you don't have to store individual bits in an array ... The string Conly takes up as many bits as it needs. ...
    (comp.lang.perl.misc)