9
str_replace does not compare the whole string, only compares half the string.
I have an associative array:
$arrayFrutas = [
"banana" => f.banana,
"bananaGrande" => f.banana_grande
];
There I have a string:
$stringParameters = "bananaGrande = 1";
I want to replace the bananaGrande of $stringParameters
for f. banana_grande of $arrayFrutas
.
For that I take the key to the $arrayFrutas
and I look for her occurrences inside the $stringParameters
, then I created a method that is just below:
public function pegarCampoCorretoAliasFilter($string, $classPerm)
{
foreach($classPerm as $key => $value) {
$string = str_replace($key, $classPerm[$key], $string);
}
return $string;
}
Where in my method pegarCampoCorretoAliasFilter($string, $classPerm){}
:
$string = $stringParameters and $classPerm = $arrayFruits.
Here is the problem that is happening instead of just replace the bananaGrande by f.banana_grande he makes an ante exchange because he finds the word banana in the string that I pass there the final result is as follows:
f.f.banan_grande
Which means I don’t want him to compare (banana = bananaGrande) because in addition not being true there is not even a space in the string, I want it to compare the string and swap it if the entire string until its space is equal to the key I am passing, producing this output:
f. banana_grande
How could I fix this ?
I will test here, but I think the problem is solved, I knew I would have to use regular expressions, but honestly I have no knowledge on this subject.
– Lucas Lima
You don’t actually need to use,
"banana" => f.banana
Why are you like this ? Stop quotation marks ?– NoobSaibot
I control the allowed fields within a constant array, so I need to go through all the fields, so I need to compare the banana => f.banana.
– Lucas Lima
but f. banana shouldn’t be inside the quotes ? "f. banana"
– NoobSaibot
I would trade "You will have to use " for "you can use".. This is a valid alternative, but it is not the only way to solve.
– Caique Romero
@Caiqueromero you all right, I had researched and found this as the only way out. Whereas there are others the phrase loses meaning, editei la.
– Luiz Santos
good, upvote ^^.
– Caique Romero
Thanks! Whenever you see something like that point me out :).
– Luiz Santos