Re: ACK! str_replace and arrays not working?
From: Paul Barfoot (Paul_at_theglobalfamily.fsworld.co.uk)
Date: 10/07/04
- Next message: frusdniw: "large includes vs small includes with lots of reads"
- Previous message: Joseph Crawford: "Re: Newsgroups Space"
- In reply to: Joel C Bennett: "ACK! str_replace and arrays not working?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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"; > }
- Next message: frusdniw: "large includes vs small includes with lots of reads"
- Previous message: Joseph Crawford: "Re: Newsgroups Space"
- In reply to: Joel C Bennett: "ACK! str_replace and arrays not working?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|