PHP Replace text to random array from substitutes
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
add a comment |
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
add a comment |
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
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
php string str-replace
asked Nov 24 '18 at 8:35
lyndrialyndria
813
813
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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);
How to make this spun result unique ?
– lyndria
Nov 26 '18 at 1:15
add a comment |
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
in This case you no need to do any changes in your substitutes
– RBT
Nov 24 '18 at 8:58
add a comment |
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!
add a comment |
How to make this spun result unique ?
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
How to make this spun result unique ?
– lyndria
Nov 26 '18 at 1:15
add a comment |
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);
How to make this spun result unique ?
– lyndria
Nov 26 '18 at 1:15
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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
in This case you no need to do any changes in your substitutes
– RBT
Nov 24 '18 at 8:58
add a comment |
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
in This case you no need to do any changes in your substitutes
– RBT
Nov 24 '18 at 8:58
add a comment |
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
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
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
add a comment |
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
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
edited Nov 24 '18 at 9:02
answered Nov 24 '18 at 8:50
Joseph_JJoseph_J
3,2702620
3,2702620
add a comment |
add a comment |
How to make this spun result unique ?
add a comment |
How to make this spun result unique ?
add a comment |
How to make this spun result unique ?
How to make this spun result unique ?
answered Nov 26 '18 at 10:16
lyndrialyndria
813
813
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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