Jois Jois - 5 months ago 22
PHP Question

Php replace string not working

I am trying to replace a particular string from a file but I am not able to.This is my php code:



$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' );


$strArr = json_encode($array);

echo "str: ".$str."\n";
echo "strArr: ".$strArr."\n";

if( str_replace($str, $strArr, $contactList)){
echo str_replace($str, $strArr, $contactList);
echo "couldnt find the match";




This is the json present in the file:

"name": "Aaron Paul"






$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?


You have a few problems with your code from where I see it. Lets review what you are tryoing to do:

$str= [{"name":"Aaron Paul"}]
 $strArr= [{"name":"Aaron Paul"},{"name":"Sumanth"}]
  $contactList={ "A":[ { "name": "Aaron Paul" } ], "B":[ { "name":"Beyonce" } ] }

From my understanding, you want to take $str, search for that specific string inside $contactList, and then replace all instances of that string with $strArr. The first problem I noticed is that you did not use str_replace() correctly. You need to use the wildcard character: % to define the limits of $str. For example:

//if you had the following string:
$string = 'abcdeafg';
//and you wanted to replace all the instances
//of 'a' with 'z'
$str1 = 'a';
$str2 = 'z';
//you would then need to use '%' as follows:
$result = str_replace("%{$str1}%", "{str2}", $string);
echo $result;//output: zbcdezfg

So in your case, your code should look like this:

$result = str_replace("%{$str}%",  "{$strArr}", $contactList);

HOWEVER, you have another problem in your code. I am noticing that the string inside $str does not exactly match the string inside $contactList because you have additional spaces inside $contactList. So you would also have to 2 one of the following things (along with the previous code correction:

  • Either somehow make sure $contactList has the exact same string as $str inside it.

  • Or use regex with preg_replace() to create a more advanced search, though that is a bit more complicated and if you don't know regex will demand some tutorial time :).

Edited: I just noticed the json_decode being used on $contactList. If you place json_decode after the str_replace function and then use my code, then $contactList will no longer have the spaces and the function should work fine :)