Problem with str_replace php

Asked

Viewed 745 times

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 ?

2 answers

7


You can use regular expression. b requires searching for the full word.

$text = preg_replace('/\bbananaGrande\b/', 'NEW', $text);

If the text contains UTF-8 you will have to do so::

$text = preg_replace('/\bbananaGrande\b/u', 'NEW', $text);

Getting something close to that:

$text = "bananaGrande = 1"; $text2 = preg_replace('/\bbanana\b/', 'NEW', $text);

$text = preg_replace('/\bbananaGrande\b/', 'NEW', $text);


echo($text2 . '<br />'); echo($text);

As you can see he only replaces when he found the full word:

inserir a descrição da imagem aqui

  • 1

    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.

  • You don’t actually need to use, "banana" => f.banana Why are you like this ? Stop quotation marks ?

  • 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.

  • but f. banana shouldn’t be inside the quotes ? "f. banana"

  • 2

    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.

  • @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.

  • 2

    good, upvote ^^.

  • Thanks! Whenever you see something like that point me out :).

Show 3 more comments

4

No, you do not need REGEX, you can use the strtr, he has a different behavior than str_replace:

$arrayFrutas = [
    "banana" => 'f.banana',
    "bananaGrande" => 'f.banana_grande'
];


$string = 'bananaGrande = 1';

echo strtr($string, $arrayFrutas);

Upshot:

f.banana_grande = 1

You can provide this explanation (by Soen) on the difference between the str_replace and the strtr.

  • 1

    I read here, thank you very much, I will use strtr, it goes faster than the REGEX.

  • I even tested this option and returned me some capital characters, other lowercase, so I went back to using the regular expression that returned the data in the correct way.

  • @Lucaslima, has to give an example of a situation where this occurs?

  • yes I will command here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.