phpphp-7.3

str_ireplace doesn’t works as expected


$venue_city = 'PARIS';
$venue_name = 'Le Palais des Congrès de Paris';

I create an array with omit words

$omit_words = array('d\''.ltrim($venue_city),'de '.$venue_city);  

print $omit_words; gives Array ( [0] => d'PARIS [1] => de PARIS )

Now i want to replace de Paris from $venue_name by nothing

$venue_name_shorten = str_ireplace($omit_words,'',$venue_name);

str_ireplace is important because i need insensitive case. Paris could be written with one or more uppercase.

So, nothing happened.

But i know that comes from my $omit_words, because if i replace 'de '.$venue_city by 'de PARIS' it works fine.

$venue_city is string type.

Any help will be appreciate.

EDIT

$venue_city is not defined like in my question $venue_city = 'PARIS'; but comes from api data

EDIT II

I found !! There was a space left before $venue_city


Solution

  • Use trim() to remove all leading and trailing spaces and line breaks from the API data.

    $fromApi = " PARIS\n";  //example
    
    $venue_city = trim($fromApi);
    
    $venue_name = 'Le Palais des Congrès de Paris';
    $omit_words = array('d\''.$venue_city,'de '.$venue_city);  
    $venue_name_shorten = str_ireplace($omit_words,'',$venue_name);