I am trying to replace a particular string from a file but I am not able to.This is my php code:
<?php
session_start();
$name = $_GET["Username"];
$status = $_GET["Status"];
$username = "jois";
if($status == "Following"){
$filename = $username."/contacts.txt";
$contactList = file_get_contents($filename);
$object = json_decode($contactList,TRUE);
$array = $object["A"];
$str = json_encode($array);
$new = array('name' => 'Sumanth' );
array_push($array,$new);
$strArr = json_encode($array);
echo "str: ".$str."\n";
echo "strArr: ".$strArr."\n";
if( str_replace($str, $strArr, $contactList)){
echo str_replace($str, $strArr, $contactList);
}
else{
echo "couldnt find the match";
}
}
else{
}
?>
This is the json present in the file:
{
"A":[
{
"name": "Aaron Paul"
}
],
"B":[
{
"name":"Beyonce"
}
]
}
EDIT:
$str= [{"name":"Aaron Paul"}]
$strArr= [{"name":"Aaron Paul"},{"name":"Sumanth"}]
$contactList={ "A":[ { "name": "Aaron Paul" } ], "B":[ { "name":"Beyonce" } ] }
I want to replace the contents of the file . Here I am trying to replace contents in the Array A. and This is the above code I am trying o use to replace contents of Array A with a new String. But I am not able to it still remains the same.I am not getting any error. Can I know where I am going wrong?