0

Note: I have seen this question here, but my array is completely different.

Hello everyone. I have been trying to make a censoring program for my website. What I ended up with was:

$wordsList = [
    array("frog","sock"),
    array("Nock","crock"),
];

$message = str_replace($wordsList[0], $wordsList[1], "frog frog Nock Nock");
echo $message;

What I am trying to do is replace "frog" with "sock" using multidimentional arrays without typing all of the words out in str_replace();

Expected Output: "sock sock crocs crocs"

However, when I execute it, for some unknown reason it doesn't actually replace the words, without any errors. I think it's a rookie mistake that I made, but I have searched and have not found any documentation on using a system like this. Please help!

Neon Foxer
  • 17
  • 6
  • What are you expecting to be replaced with what? Please explain the data and show us the expected result so we don't need to assume and guess (and answer the wrong question) – M. Eriksson Sep 11 '21 at 17:47
  • I am trying to replace the words in $wordsLIst[0] that are in the message "frog frog Nock Nock" with $wordsList[1], but it doesn't work – Neon Foxer Sep 11 '21 at 17:49

1 Answers1

-1

You need to change the structure of your wordsList array.

There are two structures that will make it easy:

As key/value pairs

This would be my recommendation since it's super clear what the strings and their replacements are.

// Store them as key/value pairs with the search and replacement strings
$wordsList = [
    'frog' => 'sock',
    'Nock' => 'crock',
];

$message = str_replace(
    array_keys($wordsList), // Get all keys as the search array
    $wordsList,             // The replacements
    "frog frog Nock Nock"
);

Demo: https://3v4l.org/WsUdn

As a multidimensional array

This one requires you to add the search/replacement values in the same order, which can be hard to read when you have a few different strings.

$wordsList = [
    ['frog', 'Nock'],  // All search strings
    ['sock', 'crock'], // All replacements
];

$message = str_replace(
    $wordsList[0], // All search strings
    $wordsList[1], // The replacements strings
    "frog frog Nock Nock"
);

Demo: https://3v4l.org/RQjC6

If you can't change the original array, then create a new array with the correct structure since that won't work "as is".

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • But I am trying to replace Nock Nock too. Is there an automatic way to do that? – Neon Foxer Sep 11 '21 at 18:01
  • That's why I asked you to give us a proper example of the expected output. – M. Eriksson Sep 11 '21 at 18:03
  • I am so sorry, I added it. English is not my main language so I cant understand some things. – Neon Foxer Sep 11 '21 at 18:04
  • @NeonFoxer - I've updated the answer with two possible solutions. You will need to use an array with a different structure, unless you want to loop through the words list and call str_replace() multiple times, but then you should rather create the arrays properly instead. – M. Eriksson Sep 11 '21 at 18:13