Dizeleri karşılaştırma yaparken PHP Olgu Hassasiyet Ignore?

3 Cevap

Im trying to compare words for equality, and the case [upper and lower] is irrelevant. However PHP does not seem to agree!

Onları karşılaştırma yaparken kelimelerin harf görmezden PHP zorlamak için nasıl Herhangi bir fikir?

Herhangi bir yardım mutluluk!

$arr_query_words = array( "hat","Cat","sAt","maT" );
// for each element in $arr_query_words -
for( $j= 0; $j < count( $arr_query_words ); $j++ ){

    // Split the $query_string on "_" or "%" :
    $story_body = str_replace( $arr_query_words[ $j ],
         '<span style=" background-color:yellow; ">' . $arr_query_words[ $j ] . '</span>',
               $story_body );

// --- This ONLY replaces where the case [upper or lower] is identical ->
}

Durum farklı olsa bile yerine yürütmek için bir yolu var mı??

Orijinal soru belirsizlik için özür dilemek.

Donal

3 Cevap

) str_ireplace to perform a case-insensitive string replacement (str_ireplace PHP 5 kullanılabilir kullanarak:

$story_body = str_ireplace($arr_query_words[$j],
   '<span style=" background-color:yellow; ">'. $arr_query_words[$j]. '</span>',
    $story_body);

, Dizeleri karşılaştırmak insensitively-küçük harf kullanın strcasecmp :

<?php
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
    echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>

Ayrıca hatırlamak kolay olabilir bu yöntemi kullanabilirsiniz

if(strtolower($var1) == strtolower($var2)) {
    // Equals, case ignored
}

Bunu gibi değerlerini kırpmak isteyebilirsiniz

if(strtolower(trim($var1)) == strtolower(trim($var2))) {
    // Equals, case ignored and values trimmed
}

Umarım bu yardımcı olur