Im trying to run a str_replace on a 'dynamic string' coming from a user.
But everytime my function is run, it seems to output the wrong thing and i can't figure out how to fix it.
So for instance, if $payload['Name'] = 'this name'
Then when the function returns the string turns into
this name Placethis name
What seems to be happening is that its also replacing the Name inside the PlaceName in the array.
My code is as follows;
function formatThis($payload)
{
$description = '';
if ( array_key_exists('Description', $payload) ) {
$description = $payload['Description'];
}
$placeName = '';
if ( array_key_exists('PlaceName', $payload) ) {
$placeName = $payload['PlaceName'];
}
$dynamicString = "Name PlaceName"
$template = str_replace(
array("Name", "Description", "PlaceName"),
array($payload['Name'], $description, $placeName),
$dynamicString
);
return $template;
}