PHP Replace text to random array from substitutes












1















How to replace "City1" with random city on $substitutes



<?php 
$placeholders = 'City1 - City2 - City3 - City4';
$substitutes = [
'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
'City2' => ['Jakarta,Bandung,Surabaya'],
'City3' => ['Atlanta,Tampa,Miami'],
'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
];
$replacements = ;
foreach($substitutes as $key => $choices) {
$random_key = array_rand($choices);
$replacements[$key] = $choices[$random_key];
}
$spun = str_replace(
array_keys($replacements),
array_values($replacements),
$placeholders
);
echo $spun;
?>


And some output: Dallas - Jakarta - Miami - Mandalay










share|improve this question



























    1















    How to replace "City1" with random city on $substitutes



    <?php 
    $placeholders = 'City1 - City2 - City3 - City4';
    $substitutes = [
    'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
    'City2' => ['Jakarta,Bandung,Surabaya'],
    'City3' => ['Atlanta,Tampa,Miami'],
    'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
    ];
    $replacements = ;
    foreach($substitutes as $key => $choices) {
    $random_key = array_rand($choices);
    $replacements[$key] = $choices[$random_key];
    }
    $spun = str_replace(
    array_keys($replacements),
    array_values($replacements),
    $placeholders
    );
    echo $spun;
    ?>


    And some output: Dallas - Jakarta - Miami - Mandalay










    share|improve this question

























      1












      1








      1








      How to replace "City1" with random city on $substitutes



      <?php 
      $placeholders = 'City1 - City2 - City3 - City4';
      $substitutes = [
      'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
      'City2' => ['Jakarta,Bandung,Surabaya'],
      'City3' => ['Atlanta,Tampa,Miami'],
      'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
      ];
      $replacements = ;
      foreach($substitutes as $key => $choices) {
      $random_key = array_rand($choices);
      $replacements[$key] = $choices[$random_key];
      }
      $spun = str_replace(
      array_keys($replacements),
      array_values($replacements),
      $placeholders
      );
      echo $spun;
      ?>


      And some output: Dallas - Jakarta - Miami - Mandalay










      share|improve this question














      How to replace "City1" with random city on $substitutes



      <?php 
      $placeholders = 'City1 - City2 - City3 - City4';
      $substitutes = [
      'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
      'City2' => ['Jakarta,Bandung,Surabaya'],
      'City3' => ['Atlanta,Tampa,Miami'],
      'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
      ];
      $replacements = ;
      foreach($substitutes as $key => $choices) {
      $random_key = array_rand($choices);
      $replacements[$key] = $choices[$random_key];
      }
      $spun = str_replace(
      array_keys($replacements),
      array_values($replacements),
      $placeholders
      );
      echo $spun;
      ?>


      And some output: Dallas - Jakarta - Miami - Mandalay







      php string str-replace






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 8:35









      lyndrialyndria

      813




      813
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Your $substitutes array is not defined correctly. Try:



          $substitutes = [
          'City1' => ['Orlando', 'Dallas', 'Atlanta', 'Detroit'],
          'City2' => ['Jakarta', 'Bandung', 'Surabaya'],
          'City3' => ['Atlanta', 'Tampa', 'Miami'],
          'City4' => ['Mandalay', 'Caloocan', 'Hai Phong', 'Quezon City']
          ];


          Or if, for some reason, you can't change how $substitutes is defined, you can do the following to transform it into the proper form:



          $substitutes = array_map(function ($cities) {
          return explode(',', $cities[0]);
          }, $substitutes);





          share|improve this answer


























          • How to make this spun result unique ?

            – lyndria
            Nov 26 '18 at 1:15



















          0














          Try This



           <?php 
          $placeholders = 'City1 - City2 - City3 - City4';
          $substitutes = [
          'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
          'City2' => ['Jakarta,Bandung,Surabaya'],
          'City3' => ['Atlanta,Tampa,Miami'],
          'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
          ];
          $replacements = ;

          foreach($substitutes as $key => $choices) {
          $element = $choices[0];
          $elements=explode(',',$element);
          $randomElement = $elements[array_rand($elements, 1)];


          $placeholders= str_replace($key, $randomElement ,$placeholders);

          }
          echo $placeholders;
          ?>


          It will produce the output as



          Orlando - Bandung - Tampa - Hai Phong





          share|improve this answer
























          • in This case you no need to do any changes in your substitutes

            – RBT
            Nov 24 '18 at 8:58



















          0














          You can do that this way as well.



          $substitutes  = [
          'City1' => ['Orlando','Dallas','Atlanta','Detroit'],
          'City2' => ['Jakarta','Bandung','Surabaya'],
          'City3' => ['Atlanta','Tampa','Miami'],
          'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
          ];

          foreach($substitutes as $city=>$cities){

          $results = $substitutes[$city][array_rand($cities)];

          }

          echo '<pre>';
          print_r($results);
          echo '</pre>';


          This will output:



          Array
          (
          [0] => Atlanta
          [1] => Bandung
          [2] => Miami
          [3] => Hai Phong
          )


          You can add this line and it will output it as a string if you want.



          $string = implode(' - ', $results);
          echo $string;


          Like so:



          Atlanta - Bandung - Miami - Hai Phong


          Good luck!






          share|improve this answer

































            0














            How to make this spun result unique ?






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456523%2fphp-replace-text-to-random-array-from-substitutes%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Your $substitutes array is not defined correctly. Try:



              $substitutes = [
              'City1' => ['Orlando', 'Dallas', 'Atlanta', 'Detroit'],
              'City2' => ['Jakarta', 'Bandung', 'Surabaya'],
              'City3' => ['Atlanta', 'Tampa', 'Miami'],
              'City4' => ['Mandalay', 'Caloocan', 'Hai Phong', 'Quezon City']
              ];


              Or if, for some reason, you can't change how $substitutes is defined, you can do the following to transform it into the proper form:



              $substitutes = array_map(function ($cities) {
              return explode(',', $cities[0]);
              }, $substitutes);





              share|improve this answer


























              • How to make this spun result unique ?

                – lyndria
                Nov 26 '18 at 1:15
















              1














              Your $substitutes array is not defined correctly. Try:



              $substitutes = [
              'City1' => ['Orlando', 'Dallas', 'Atlanta', 'Detroit'],
              'City2' => ['Jakarta', 'Bandung', 'Surabaya'],
              'City3' => ['Atlanta', 'Tampa', 'Miami'],
              'City4' => ['Mandalay', 'Caloocan', 'Hai Phong', 'Quezon City']
              ];


              Or if, for some reason, you can't change how $substitutes is defined, you can do the following to transform it into the proper form:



              $substitutes = array_map(function ($cities) {
              return explode(',', $cities[0]);
              }, $substitutes);





              share|improve this answer


























              • How to make this spun result unique ?

                – lyndria
                Nov 26 '18 at 1:15














              1












              1








              1







              Your $substitutes array is not defined correctly. Try:



              $substitutes = [
              'City1' => ['Orlando', 'Dallas', 'Atlanta', 'Detroit'],
              'City2' => ['Jakarta', 'Bandung', 'Surabaya'],
              'City3' => ['Atlanta', 'Tampa', 'Miami'],
              'City4' => ['Mandalay', 'Caloocan', 'Hai Phong', 'Quezon City']
              ];


              Or if, for some reason, you can't change how $substitutes is defined, you can do the following to transform it into the proper form:



              $substitutes = array_map(function ($cities) {
              return explode(',', $cities[0]);
              }, $substitutes);





              share|improve this answer















              Your $substitutes array is not defined correctly. Try:



              $substitutes = [
              'City1' => ['Orlando', 'Dallas', 'Atlanta', 'Detroit'],
              'City2' => ['Jakarta', 'Bandung', 'Surabaya'],
              'City3' => ['Atlanta', 'Tampa', 'Miami'],
              'City4' => ['Mandalay', 'Caloocan', 'Hai Phong', 'Quezon City']
              ];


              Or if, for some reason, you can't change how $substitutes is defined, you can do the following to transform it into the proper form:



              $substitutes = array_map(function ($cities) {
              return explode(',', $cities[0]);
              }, $substitutes);






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 24 '18 at 8:46

























              answered Nov 24 '18 at 8:40









              JetoJeto

              5,88821119




              5,88821119













              • How to make this spun result unique ?

                – lyndria
                Nov 26 '18 at 1:15



















              • How to make this spun result unique ?

                – lyndria
                Nov 26 '18 at 1:15

















              How to make this spun result unique ?

              – lyndria
              Nov 26 '18 at 1:15





              How to make this spun result unique ?

              – lyndria
              Nov 26 '18 at 1:15













              0














              Try This



               <?php 
              $placeholders = 'City1 - City2 - City3 - City4';
              $substitutes = [
              'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
              'City2' => ['Jakarta,Bandung,Surabaya'],
              'City3' => ['Atlanta,Tampa,Miami'],
              'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
              ];
              $replacements = ;

              foreach($substitutes as $key => $choices) {
              $element = $choices[0];
              $elements=explode(',',$element);
              $randomElement = $elements[array_rand($elements, 1)];


              $placeholders= str_replace($key, $randomElement ,$placeholders);

              }
              echo $placeholders;
              ?>


              It will produce the output as



              Orlando - Bandung - Tampa - Hai Phong





              share|improve this answer
























              • in This case you no need to do any changes in your substitutes

                – RBT
                Nov 24 '18 at 8:58
















              0














              Try This



               <?php 
              $placeholders = 'City1 - City2 - City3 - City4';
              $substitutes = [
              'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
              'City2' => ['Jakarta,Bandung,Surabaya'],
              'City3' => ['Atlanta,Tampa,Miami'],
              'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
              ];
              $replacements = ;

              foreach($substitutes as $key => $choices) {
              $element = $choices[0];
              $elements=explode(',',$element);
              $randomElement = $elements[array_rand($elements, 1)];


              $placeholders= str_replace($key, $randomElement ,$placeholders);

              }
              echo $placeholders;
              ?>


              It will produce the output as



              Orlando - Bandung - Tampa - Hai Phong





              share|improve this answer
























              • in This case you no need to do any changes in your substitutes

                – RBT
                Nov 24 '18 at 8:58














              0












              0








              0







              Try This



               <?php 
              $placeholders = 'City1 - City2 - City3 - City4';
              $substitutes = [
              'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
              'City2' => ['Jakarta,Bandung,Surabaya'],
              'City3' => ['Atlanta,Tampa,Miami'],
              'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
              ];
              $replacements = ;

              foreach($substitutes as $key => $choices) {
              $element = $choices[0];
              $elements=explode(',',$element);
              $randomElement = $elements[array_rand($elements, 1)];


              $placeholders= str_replace($key, $randomElement ,$placeholders);

              }
              echo $placeholders;
              ?>


              It will produce the output as



              Orlando - Bandung - Tampa - Hai Phong





              share|improve this answer













              Try This



               <?php 
              $placeholders = 'City1 - City2 - City3 - City4';
              $substitutes = [
              'City1' => ['Orlando,Dallas,Atlanta,Detroit'],
              'City2' => ['Jakarta,Bandung,Surabaya'],
              'City3' => ['Atlanta,Tampa,Miami'],
              'City4' => ['Mandalay,Caloocan,Hai Phong,Quezon City'],
              ];
              $replacements = ;

              foreach($substitutes as $key => $choices) {
              $element = $choices[0];
              $elements=explode(',',$element);
              $randomElement = $elements[array_rand($elements, 1)];


              $placeholders= str_replace($key, $randomElement ,$placeholders);

              }
              echo $placeholders;
              ?>


              It will produce the output as



              Orlando - Bandung - Tampa - Hai Phong






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 24 '18 at 8:53









              RBTRBT

              36513




              36513













              • in This case you no need to do any changes in your substitutes

                – RBT
                Nov 24 '18 at 8:58



















              • in This case you no need to do any changes in your substitutes

                – RBT
                Nov 24 '18 at 8:58

















              in This case you no need to do any changes in your substitutes

              – RBT
              Nov 24 '18 at 8:58





              in This case you no need to do any changes in your substitutes

              – RBT
              Nov 24 '18 at 8:58











              0














              You can do that this way as well.



              $substitutes  = [
              'City1' => ['Orlando','Dallas','Atlanta','Detroit'],
              'City2' => ['Jakarta','Bandung','Surabaya'],
              'City3' => ['Atlanta','Tampa','Miami'],
              'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
              ];

              foreach($substitutes as $city=>$cities){

              $results = $substitutes[$city][array_rand($cities)];

              }

              echo '<pre>';
              print_r($results);
              echo '</pre>';


              This will output:



              Array
              (
              [0] => Atlanta
              [1] => Bandung
              [2] => Miami
              [3] => Hai Phong
              )


              You can add this line and it will output it as a string if you want.



              $string = implode(' - ', $results);
              echo $string;


              Like so:



              Atlanta - Bandung - Miami - Hai Phong


              Good luck!






              share|improve this answer






























                0














                You can do that this way as well.



                $substitutes  = [
                'City1' => ['Orlando','Dallas','Atlanta','Detroit'],
                'City2' => ['Jakarta','Bandung','Surabaya'],
                'City3' => ['Atlanta','Tampa','Miami'],
                'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
                ];

                foreach($substitutes as $city=>$cities){

                $results = $substitutes[$city][array_rand($cities)];

                }

                echo '<pre>';
                print_r($results);
                echo '</pre>';


                This will output:



                Array
                (
                [0] => Atlanta
                [1] => Bandung
                [2] => Miami
                [3] => Hai Phong
                )


                You can add this line and it will output it as a string if you want.



                $string = implode(' - ', $results);
                echo $string;


                Like so:



                Atlanta - Bandung - Miami - Hai Phong


                Good luck!






                share|improve this answer




























                  0












                  0








                  0







                  You can do that this way as well.



                  $substitutes  = [
                  'City1' => ['Orlando','Dallas','Atlanta','Detroit'],
                  'City2' => ['Jakarta','Bandung','Surabaya'],
                  'City3' => ['Atlanta','Tampa','Miami'],
                  'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
                  ];

                  foreach($substitutes as $city=>$cities){

                  $results = $substitutes[$city][array_rand($cities)];

                  }

                  echo '<pre>';
                  print_r($results);
                  echo '</pre>';


                  This will output:



                  Array
                  (
                  [0] => Atlanta
                  [1] => Bandung
                  [2] => Miami
                  [3] => Hai Phong
                  )


                  You can add this line and it will output it as a string if you want.



                  $string = implode(' - ', $results);
                  echo $string;


                  Like so:



                  Atlanta - Bandung - Miami - Hai Phong


                  Good luck!






                  share|improve this answer















                  You can do that this way as well.



                  $substitutes  = [
                  'City1' => ['Orlando','Dallas','Atlanta','Detroit'],
                  'City2' => ['Jakarta','Bandung','Surabaya'],
                  'City3' => ['Atlanta','Tampa','Miami'],
                  'City4' => ['Mandalay','Caloocan','Hai Phong','Quezon City'],
                  ];

                  foreach($substitutes as $city=>$cities){

                  $results = $substitutes[$city][array_rand($cities)];

                  }

                  echo '<pre>';
                  print_r($results);
                  echo '</pre>';


                  This will output:



                  Array
                  (
                  [0] => Atlanta
                  [1] => Bandung
                  [2] => Miami
                  [3] => Hai Phong
                  )


                  You can add this line and it will output it as a string if you want.



                  $string = implode(' - ', $results);
                  echo $string;


                  Like so:



                  Atlanta - Bandung - Miami - Hai Phong


                  Good luck!







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 9:02

























                  answered Nov 24 '18 at 8:50









                  Joseph_JJoseph_J

                  3,2702620




                  3,2702620























                      0














                      How to make this spun result unique ?






                      share|improve this answer




























                        0














                        How to make this spun result unique ?






                        share|improve this answer


























                          0












                          0








                          0







                          How to make this spun result unique ?






                          share|improve this answer













                          How to make this spun result unique ?







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 10:16









                          lyndrialyndria

                          813




                          813






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456523%2fphp-replace-text-to-random-array-from-substitutes%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown