PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

substr_count> <strtr
Last updated: Fri, 22 Aug 2008

view this page in

substr_compare

(PHP 5)

substr_compareComparación de 2 cadenas, segura con material binario, opcionalmente insensible a mayúsculas y minúsculas, a partir de un desplazamiento, y hasta un número límite de caracteres

Descripción

int substr_compare ( string $cadena_principal , string $cadena , int $desplazamiento [, int $longitud [, bool $insensibilidad_mayusculas ]] )

substr_compare() compara cadena_principal desde la posición desplazamiento con cadena hasta tantos caracteres como el valor de longitud .

Lista de parámetros

cadena_principal

cadena

desplazamiento

La posición de inicio para la comparación. Si es un valor negativo, comienza contando desde el final de la cadena.

longitud

La longitud de la comparación.

insensibilidad_mayusculas

Si insensibilidad_mayusculas es TRUE, la comparación es insensible a mayúsculas y minúsculas.

Valores retornados

Devuelve < 0 si cadena_principal desde la posición desplazamiento es menor que cadena , > 0 si es mayor que cadena , y 0 si son iguales. Si longitud es igual o mayor que la longitud de cadena_principal y longitud se define, substr_compare() imprime una advertencia y devuelve FALSE.

Registro de cambios

Versión Descripción
5.1.0 Se agregó la posibilidad de usar un desplazamiento negativo.

Ejemplos

Example #1 Un ejemplo de substr_compare()

<?php
echo substr_compare("abcde""bc"12); // 0
echo substr_compare("abcde""de", -22); // 0
echo substr_compare("abcde""bcg"12); // 0
echo substr_compare("abcde""BC"12true); // 0
echo substr_compare("abcde""bc"13); // 1
echo substr_compare("abcde""cd"12); // -1
echo substr_compare("abcde""abc"51); // advertencia
?>



add a note add a note User Contributed Notes
substr_compare
sleek
23-May-2005 02:07
Modified version of the original posted function. Use this one:

<?php
if (!function_exists('substr_compare')) {
    function
substr_compare($main_str, $str, $offset, $length = NULL, $case_insensitivity = false) {
       
$offset = (int) $offset;

       
// Throw a warning because the offset is invalid
       
if ($offset >= strlen($main_str)) {
           
trigger_error('The start position cannot exceed initial string length.', E_USER_WARNING);
            return
false;
        }

       
// We are comparing the first n-characters of each string, so let's use the PHP function to do it
       
if ($offset == 0 && is_int($length) && $case_insensitivity === true) {
            return
strncasecmp($main_str, $str, $length);
        }

       
// Get the substring that we are comparing
       
if (is_int($length)) {
           
$main_substr = substr($main_str, $offset, $length);
           
$str_substr = substr($str, 0, $length);
        } else {
           
$main_substr = substr($main_str, $offset);
           
$str_substr = $str;
        }

       
// Return a case-insensitive comparison of the two strings
       
if ($case_insensitivity === true) {
            return
strcasecmp($main_substr, $str_substr);
        }

       
// Return a case-sensitive comparison of the two strings
       
return strcmp($main_substr, $str_substr);
    }
}
?>

substr_count> <strtr
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites