str_replace, gives me the wrong result Only when numerals are in it [duplicate]
This question already has an answer here:
str_replace with array
4 answers
//post data
$post = $_POST['username'];
//str replace
$print = str_replace([1,2,3,4,5], ['4A','6B','7C','2D','6F'], $post);
//prints result
echo ""username":"" . $print . """;
str replace works fine when it's just alpha (example: TEST!@#test = 7160767124452671607671
which is just perfect, but as soon as digits come into play things get messy $print
should output "username":"4A6B7C2D6F"
if input 12345
the current output is "username":"2DA6B7C2D6F"
php str-replace
marked as duplicate by Tim Biegeleisen, Phil
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 4:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 7 more comments
This question already has an answer here:
str_replace with array
4 answers
//post data
$post = $_POST['username'];
//str replace
$print = str_replace([1,2,3,4,5], ['4A','6B','7C','2D','6F'], $post);
//prints result
echo ""username":"" . $print . """;
str replace works fine when it's just alpha (example: TEST!@#test = 7160767124452671607671
which is just perfect, but as soon as digits come into play things get messy $print
should output "username":"4A6B7C2D6F"
if input 12345
the current output is "username":"2DA6B7C2D6F"
php str-replace
marked as duplicate by Tim Biegeleisen, Phil
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 4:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Please show us clear input and the expected output with that input. I don't follow what you are trying to do here.
– Tim Biegeleisen
Nov 14 '18 at 4:13
2
1
becomes4A
then the4
is replaced with2D
so you get the2DA
.
– user3783243
Nov 14 '18 at 4:21
1
Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples
– Phil
Nov 14 '18 at 4:24
2
Also, never roll your own JSON. Useecho json_encode(['username' => $print]);
instead
– Phil
Nov 14 '18 at 4:26
1
So what exactly does "I have alpha in my actual code, as well as symbols" mean? Are you or are you not only trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F?
– Phil
Nov 14 '18 at 4:32
|
show 7 more comments
This question already has an answer here:
str_replace with array
4 answers
//post data
$post = $_POST['username'];
//str replace
$print = str_replace([1,2,3,4,5], ['4A','6B','7C','2D','6F'], $post);
//prints result
echo ""username":"" . $print . """;
str replace works fine when it's just alpha (example: TEST!@#test = 7160767124452671607671
which is just perfect, but as soon as digits come into play things get messy $print
should output "username":"4A6B7C2D6F"
if input 12345
the current output is "username":"2DA6B7C2D6F"
php str-replace
This question already has an answer here:
str_replace with array
4 answers
//post data
$post = $_POST['username'];
//str replace
$print = str_replace([1,2,3,4,5], ['4A','6B','7C','2D','6F'], $post);
//prints result
echo ""username":"" . $print . """;
str replace works fine when it's just alpha (example: TEST!@#test = 7160767124452671607671
which is just perfect, but as soon as digits come into play things get messy $print
should output "username":"4A6B7C2D6F"
if input 12345
the current output is "username":"2DA6B7C2D6F"
This question already has an answer here:
str_replace with array
4 answers
php str-replace
php str-replace
asked Nov 14 '18 at 4:11
Razu RazuRazu Razu
12
12
marked as duplicate by Tim Biegeleisen, Phil
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 4:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Tim Biegeleisen, Phil
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 4:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Please show us clear input and the expected output with that input. I don't follow what you are trying to do here.
– Tim Biegeleisen
Nov 14 '18 at 4:13
2
1
becomes4A
then the4
is replaced with2D
so you get the2DA
.
– user3783243
Nov 14 '18 at 4:21
1
Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples
– Phil
Nov 14 '18 at 4:24
2
Also, never roll your own JSON. Useecho json_encode(['username' => $print]);
instead
– Phil
Nov 14 '18 at 4:26
1
So what exactly does "I have alpha in my actual code, as well as symbols" mean? Are you or are you not only trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F?
– Phil
Nov 14 '18 at 4:32
|
show 7 more comments
2
Please show us clear input and the expected output with that input. I don't follow what you are trying to do here.
– Tim Biegeleisen
Nov 14 '18 at 4:13
2
1
becomes4A
then the4
is replaced with2D
so you get the2DA
.
– user3783243
Nov 14 '18 at 4:21
1
Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples
– Phil
Nov 14 '18 at 4:24
2
Also, never roll your own JSON. Useecho json_encode(['username' => $print]);
instead
– Phil
Nov 14 '18 at 4:26
1
So what exactly does "I have alpha in my actual code, as well as symbols" mean? Are you or are you not only trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F?
– Phil
Nov 14 '18 at 4:32
2
2
Please show us clear input and the expected output with that input. I don't follow what you are trying to do here.
– Tim Biegeleisen
Nov 14 '18 at 4:13
Please show us clear input and the expected output with that input. I don't follow what you are trying to do here.
– Tim Biegeleisen
Nov 14 '18 at 4:13
2
2
1
becomes 4A
then the 4
is replaced with 2D
so you get the 2DA
.– user3783243
Nov 14 '18 at 4:21
1
becomes 4A
then the 4
is replaced with 2D
so you get the 2DA
.– user3783243
Nov 14 '18 at 4:21
1
1
Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples
– Phil
Nov 14 '18 at 4:24
Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples
– Phil
Nov 14 '18 at 4:24
2
2
Also, never roll your own JSON. Use
echo json_encode(['username' => $print]);
instead– Phil
Nov 14 '18 at 4:26
Also, never roll your own JSON. Use
echo json_encode(['username' => $print]);
instead– Phil
Nov 14 '18 at 4:26
1
1
So what exactly does "I have alpha in my actual code, as well as symbols" mean? Are you or are you not only trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F?
– Phil
Nov 14 '18 at 4:32
So what exactly does "I have alpha in my actual code, as well as symbols" mean? Are you or are you not only trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F?
– Phil
Nov 14 '18 at 4:32
|
show 7 more comments
2 Answers
2
active
oldest
votes
str_replace
starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr
which won't.
The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
E.g.:
echo strtr("12345", [
1 => '4A',
2 => '6B',
3 => '7C',
4 => '2D',
5 => '6F'
]);
will give you:
4A6B7C2D6F
add a comment |
Welcome to StackOverflow!
I found some information regarding your issue in the official PHP.net documentation.
Caution
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a
previously inserted value when doing multiple replacements. See also
the examples in this document.
In your case it's first replacing the 1
with 4A
and then replacing the 4
in 4A
with 2D
resulting in 2DA
and after that it seems to be replacing everything correctly.
Happy coding!
EDIT: Using strtr as illustrated in Paul's answer will give you the result you desire.
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
That is not my answer
– Phil
Nov 14 '18 at 4:31
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
str_replace
starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr
which won't.
The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
E.g.:
echo strtr("12345", [
1 => '4A',
2 => '6B',
3 => '7C',
4 => '2D',
5 => '6F'
]);
will give you:
4A6B7C2D6F
add a comment |
str_replace
starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr
which won't.
The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
E.g.:
echo strtr("12345", [
1 => '4A',
2 => '6B',
3 => '7C',
4 => '2D',
5 => '6F'
]);
will give you:
4A6B7C2D6F
add a comment |
str_replace
starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr
which won't.
The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
E.g.:
echo strtr("12345", [
1 => '4A',
2 => '6B',
3 => '7C',
4 => '2D',
5 => '6F'
]);
will give you:
4A6B7C2D6F
str_replace
starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr
which won't.
The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
E.g.:
echo strtr("12345", [
1 => '4A',
2 => '6B',
3 => '7C',
4 => '2D',
5 => '6F'
]);
will give you:
4A6B7C2D6F
edited Nov 14 '18 at 4:30
answered Nov 14 '18 at 4:24
PaulPaul
17.4k103857
17.4k103857
add a comment |
add a comment |
Welcome to StackOverflow!
I found some information regarding your issue in the official PHP.net documentation.
Caution
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a
previously inserted value when doing multiple replacements. See also
the examples in this document.
In your case it's first replacing the 1
with 4A
and then replacing the 4
in 4A
with 2D
resulting in 2DA
and after that it seems to be replacing everything correctly.
Happy coding!
EDIT: Using strtr as illustrated in Paul's answer will give you the result you desire.
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
That is not my answer
– Phil
Nov 14 '18 at 4:31
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
add a comment |
Welcome to StackOverflow!
I found some information regarding your issue in the official PHP.net documentation.
Caution
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a
previously inserted value when doing multiple replacements. See also
the examples in this document.
In your case it's first replacing the 1
with 4A
and then replacing the 4
in 4A
with 2D
resulting in 2DA
and after that it seems to be replacing everything correctly.
Happy coding!
EDIT: Using strtr as illustrated in Paul's answer will give you the result you desire.
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
That is not my answer
– Phil
Nov 14 '18 at 4:31
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
add a comment |
Welcome to StackOverflow!
I found some information regarding your issue in the official PHP.net documentation.
Caution
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a
previously inserted value when doing multiple replacements. See also
the examples in this document.
In your case it's first replacing the 1
with 4A
and then replacing the 4
in 4A
with 2D
resulting in 2DA
and after that it seems to be replacing everything correctly.
Happy coding!
EDIT: Using strtr as illustrated in Paul's answer will give you the result you desire.
Welcome to StackOverflow!
I found some information regarding your issue in the official PHP.net documentation.
Caution
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a
previously inserted value when doing multiple replacements. See also
the examples in this document.
In your case it's first replacing the 1
with 4A
and then replacing the 4
in 4A
with 2D
resulting in 2DA
and after that it seems to be replacing everything correctly.
Happy coding!
EDIT: Using strtr as illustrated in Paul's answer will give you the result you desire.
edited Nov 14 '18 at 4:31
answered Nov 14 '18 at 4:26
Linus JuhlinLinus Juhlin
299413
299413
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
That is not my answer
– Phil
Nov 14 '18 at 4:31
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
add a comment |
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
That is not my answer
– Phil
Nov 14 '18 at 4:31
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
This is a nice explanation of the problem but doesn't exactly provide a solution.
– Phil
Nov 14 '18 at 4:28
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.
– Linus Juhlin
Nov 14 '18 at 4:30
That is not my answer
– Phil
Nov 14 '18 at 4:31
That is not my answer
– Phil
Nov 14 '18 at 4:31
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.
– Linus Juhlin
Nov 14 '18 at 4:32
add a comment |
2
Please show us clear input and the expected output with that input. I don't follow what you are trying to do here.
– Tim Biegeleisen
Nov 14 '18 at 4:13
2
1
becomes4A
then the4
is replaced with2D
so you get the2DA
.– user3783243
Nov 14 '18 at 4:21
1
Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples
– Phil
Nov 14 '18 at 4:24
2
Also, never roll your own JSON. Use
echo json_encode(['username' => $print]);
instead– Phil
Nov 14 '18 at 4:26
1
So what exactly does "I have alpha in my actual code, as well as symbols" mean? Are you or are you not only trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F?
– Phil
Nov 14 '18 at 4:32