I have input textarea, the text that I input is converted into $array
like:
Array
(
[0] => cat
[1] => sat
[2] => on
[3] => the
[4] => monkey
[6] => is
[7] => nice
[8] => dog
[9] => ate
[10] => fish
)
And I have $another_array
like:
Array
(
[0] => Array
(
[id] => 1
[word] => cat
[keyword] => nice cat
)
[1] => Array
(
[id] => 2
[word] => dog
[keyword] => good dog
)
[2] => Array
(
[id] => 3
[word] => monkey
[keyword] => cute monkey
)
[3] => Array
(
[id] => 4
[word] => fish
[keyword] => fresh fish
)
[4] => Array
(
[id] => 5
[word] => bird
[keyword] => love bird
)
[5] => Array
(
[id] => 6
[word] => rabbit
[keyword] => rats
)
[6] => Array
(
[id] => 7
[word] => animal
[keyword] => not animal
)
)
What I am trying to do is to replace every word in $array
that matching [word] in $another_array
and replace every words on textarea based [keyword] in $another_array
I tried to use str_replace
, but the result is so messy.
The text I input in textarea
cat sat on the monkey, monkey is nice, dog ate fish
I want a result like
nice cat sat on the cute monkey, cute monkey is nice, good dog ate fresh fish