2
How to separate values from a variable and save to separate variables?
Example:
$valores = '12';
How to separate and store in other variables? So:
$primeiroValor = 1;
$segundoValor = 2;
2
How to separate values from a variable and save to separate variables?
Example:
$valores = '12';
How to separate and store in other variables? So:
$primeiroValor = 1;
$segundoValor = 2;
4
Hello, you can use the split method, follow how it works and examples.
This method "breaks" the string using a delimiter set by you and returns an array.
Example:
Split(Pattern,string, limit)
$strExemple = "este/e/um/exemplo";
$teste = split ('/', $strExemple);
// Saída
// $teste[0] = "este";
// $teste[1] = "e";
// $teste[2] = "um";
// $teste[3] = "exemplo";
I think in this case it would be a waste... Just imagine: $strExemple = "1/2"; ?
It would be. but I took the opportunity to teach in case it happens in a different environment in which it needs to separate by other rules =)
split
is deprecated by PHP (E_DEPRECATED). Instead it should be used explode
or preg_split
(in which case the explode
)
3
Another solution would be to separate character by character so that you have each to do what you want.
Example, showing each character of a string in a row;
$palavra = "teste";
$tamPalavra = strlen($palavra);
for ($i = 0; $i < $tamPalavra; $i++) {
echo 'Caracter ' . $i . ' = ' . $palavra[$i] . '<br />';
}
The output of this code will be something like:
Character 1 = t
Character 2 = e
Character 3 = s
Character 4 = t
Character 5 = e
This code allows you to work with a string of unknown size and perform the manipulation you want freely.
3
Since it’s always two characters so just do it like this:
$valores = '12';
$primeiroValor = $valores[0];
$segundoValor = $valores[1];
Thank you all for the tips! It worked with the Earendul response! Thank you!!!
And if you value an integer ?
Then you can convert to string. Or else, as is always 2 characters, you can divide the number by 10, the entire part of the division will be the primeiroValor
and the rest of the division segundoValor
0
Both answers are right but it all depends on the way it is intended to get the desired.
So, if the intention is to separate character from character then the 'Earendul' response is the best option. However, if the character separation is not indicated, the 'Wisner Oliveira' solution is recommended... Having said that, I would like to supplement the answer by the following:
Split uses "regular Expressions" which in many cases reduces performance and from version 5.3 onwards will be a "decrepated" command so avoid.
There is the preg_split which is said to be faster between 25% to 50% when compared to Split
If the case is delimited by a character or character set then in most cases the "explode" is the most efficient solution.
I hope I’ve helped.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
These values are separated by some delimiter?
– rray
have only need to have a delimiter something like that to make sense
– Otto
Is the size always the same? In this case it is always 2 characters? If it is just do so:
$primeiroValor = $valores[0]; $segundoValor = $valores[1];
– Franchesco
in this case you would like to deal with variables of type string and not integers, correct? Strings in PHP are strings, you could pull each value by its index, then concatenate the two and pass to int
– MarceloBoni
will always be two values... will be the ID of the news...because right below I will display other news, but has to be different from the ones I have...
– user3081
Okay, it’s the news ID like you said, but there’s no way to get to the news 100 for example, so it’s gone to 3 digits?
– Marcelo Diniz