1

My previous code was a rand, which has nothing to do with my current intent, which is to replace whenever I find a value, by the other value of the array.

$myWords=array(
    array('funny','sad'),
    array('fast','slow'),
    array('beautiful','ugly'),
    array('left','right'),
    array('5','five'),
    array('strong','weak')
);

$mycontentmixed = rewrite3($myVar, $myWords);
$myVar = 'This girl is very funny and fast and kick to left';

When the system finds the value of any key contained in the array, always switch to it for another value, I have these system ready, but it does a rand that sometimes falls on the same key found, and in 50% of cases it does not Value, I would always like to change.

I want to change to:

output: 'This girl is very sad and slow and kick to right';

Or if you find another key:

$myVar = 'This girl is very sad and slow and kick to right';

Switch to:

Output: 'This girl is very funny and fast and kick to left';

When one of the keys is found in the $myVar variable always make the exchange for the other key.

Thanks.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

1

This code will do that:

$myWords=array(
array('funny','sad')
,array('fast','slow')
,array('beautiful','ugly')
,array('left','right')
,array('5','five')
,array('strong','weak')
);


$myVar = 'This girl is very funny and fast and kick to left';

foreach ($myWords as $key => $val) {
  if (strpos($myVar, $val[0]) !== FALSE) {
    $myVar = str_replace($val[0], $val[1], $myVar);
  }
  else {
    $myVar = str_replace($val[1], $val[0], $myVar);
  }
}
echo $myVar;

Hope this helps.

Pranit Jha
  • 153
  • 5
1

Barmar is right, strtr() is the right function for this task. Because the $myWords array isn't structured for immediate use with strtr(), a couple of preparatory function calls are necessary. array_column() is used to create associative arrays when the 2nd and 3rd parameters are used.

Code: (Demo)

$myVar = 'This girl is very sad and ugly so swipe left';
echo strtr(
         $myVar,
         array_column($myWords, 0, 1)
         + array_column($myWords, 1, 0)
     );

Inputs/Outputs:

   $myVar = 'This girl is very funny and fast and kick to left';
// output :  This girl is very sad and slow and kick to right

   $myVar = 'This girl is very sad and slow and kick to right';
// output :  This girl is very funny and fast and kick to left

   $myVar = 'I was beautiful and strong when I was 5 now I\'m ugly and weak';
// output :  I was ugly and weak when I was five now I'm beautiful and strong

I couldn't use array_merge() because numeric strings (e.g. 5) get scrubbed in the process. This is the reason for the + (array union operator -- not an addition operator).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @ArianeMartinsGomesDoRego The other answer makes two function calls per iteration of the `$myWords` My method does a maximum of 7 function calls total and only modifies the string once. As your `$myWords` array size increases, my solution provides increased efficiency. – mickmackusa Jun 01 '17 at 13:09
  • Great! Thanks! @mickmackusa by the improvement! How would you do for multi-valued arrays, not just a single switch? `array('funny','sad','intelligent','studious','hardworking','delicate');` ...How to make the system choose any of these options? But always make the exchange? I have it ready, but the rand often ends up on top of the word that was already in the variable, and nothing is changed! I would like not to include this value in rand, that kid found out of the rand `$myVar='This girl is very studious and delicate');` exclude delicate and studious in rand, thanks for answer. – Ariane Martins Gomes Do Rego Jun 01 '17 at 23:50
  • @ArianeMartinsGomesDoRego Please post a new sample array of words, explain how they should be replaced, and show the expected output given a specific sentence. – mickmackusa Jun 02 '17 at 00:10
  • Yes, You gave me an idea. @mickmackusa I'll separate the numerical values and make another code just to give it with numbers. In addition to including them is not important, I can easily do this work with them in another simple code. If you can look at this issue above. Thanks again :-) – Ariane Martins Gomes Do Rego Jun 02 '17 at 00:15
  • Thanks @mickmackusa, I will open new question. – Ariane Martins Gomes Do Rego Jun 02 '17 at 00:54