/    Sign up×
Community /Pin to ProfileBookmark

str_replace of array not working

I have a form to capture some data which should write the captured data to PHP variables which are then written to a template replacing the appropriate variables in the external file using str_replace array.

But…. for some reason the PHP opens the file, finds and replaces the first array item correctly but then replaces the remaining ones with NULL / Blank – as if the remaining form data is not being saved to the variables or something. Can anybody please tell me why the first array item is replaced properly but not the others and how to fix it?

[B]FORM CAPTURE PORTION OF CODE:[/B]

[code=html]<form id=”form1″ method=”post” action=””>

<div>What is the advertised name of your business?<input name=”busName” type=”text” value=”Enter Business Name” size=”40″ maxlength=”40″ style=”margin-left:10px;” /></div>

<hr />

<div>Enter the NUMERIC street number for your business:<input name=”busNume” type=”text” value=”Enter Numeric Address” size=”40″ maxlength=”40″ style=”margin-left:10px;” /></div>

<div>Please select street direction:<select name=”streetDir” size=”1″ style=”margin-left:10px;”>

<option>N</option>

<option>E</option>

<option>S</option>

<option>W</option>

<option> </option>

</select></div>

<div>Please enter the name of your street location:<input name=”busStreet” type=”text” value=”Enter Street Name” size=”40″ maxlength=”40″ style=”margin-left:10px;” /></div>

<div>Please select the street type:<select name=”streetType” size=”1″ style=”margin-left:10px;”>

<option>RD</option>

<option>ST</option>

<option>AVE</option>

</select></div>

<div>Enter your city:<input name=”busCity” type=”text” value=”Enter City Name” size=”40″ maxlength=”40″ style=”margin-left:10px;” /></div>

<div>Enter your state:<select name=”busState” size=”1″ style=”margin-left:10px;”>

<option>AL</option>

<option>AK</option>

<option>AZ</option>

</select></div>

<div>Enter your zip code:

<input name=”busZip” type=”text” value=”” size=”10″ maxlength=”5″ style=”margin-left:10px;” />

</div>

<hr />

<input type=”submit” value=”submit” >

</form>[/code]

[B]PHP Portion of code in the form capture document[/B]

[code=php]<?php

$busName = $_POST[“busName”];

$busNume = $POST[“busNume”];

$stDir = $POST[“streetDir”];

$busStreet = $POST[“busStreet”];

$stType = $POST[“streetType”];

$busCity = $POST[“busCity”];

$busState = $POST[“busState”];

$busZip = $POST[“busZip”];

?>

<?php

if (!empty($_POST))

{

$myFile = “template.php”;

$file_contents = file_get_contents($myFile);

$fh = fopen($myFile, ‘w’) or die(“can’t open file”);

$varibs = array(‘*bizname’,’*hn’,’*dir’,’*street’,’*stt’,’*busCity’,’*busState’,’*busZip’);

$details = array($busName,$busNume,$stDir,$busStreet,$stType,$busCity,$busState,$busZip);

$file_contents = str_replace($varibs,$details,$file_contents);

fwrite($fh, $file_contents);

fclose($fh);

}

?>[/code]

[B]Lastly – a snippet form the external template file (template.php) that is opened, searched and variables replaced in.[/B]

[code=html]<div id=”bname” class=”bdetail bold fs19″ itemprop=”name”>*bizname</div>
<span itemprop=”address” itemscope itemtype=”http://schema.org/PostalAddress”>
<div id=”baddr” class=”bdetail” itemprop=’streetAddress’>*hn *dir *street *stt</div>
<div id=”bregion” class=”bdetail” itemprop=’addressLocality’>*busCity, <span itemprop=’addressRegion’>*busState</span>*busZip <span itemprop=’postalCode’></span></div>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@mbaldwinMay 01.2013 — you need to loop through your array to make the replacements.

I would build the arrays as one array as well, like

[code=php]
$vars = array(
'*bizname'=>$bizname,
'*hn'=>$busNume,
'*dir'=>$stDir,
'*street'=>$busStreet,
'*stt'=>$stType,
'*busCity'=>$busCity,
'*busState'=>$busState,
'*busZip'=>$busZip,
);
[/code]


Then for the replacement, do something like this

[code=php]
$file_contents = file_get_contents($myFile);

FOREACH ($vars as $k => $v) {
$file_contents = str_replace($k,$v,$file_contents);
}
FILE_PUT_CONTENTS($myFile, $file_contents);

[/code]


Michael
×

Success!

Help @ARCLite_Studio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...