Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

substr_compare

(PHP 5)

substr_compare --  Безопасное для обработки данных в двоичной форме сравнение 2 строк со смещением, с учетом или без учета регистра

Описание

int substr_compare ( string main_str, string str, int offset [, int length [, bool case_sensitivity]] )

substr_compare() сравнивает строку main_str начиная с символа, номер которого задан аргументом offset, со строкой str. В сравнении участвуют максимум length символов.

Возвращает число < 0 если main_str начиная с символа offset меньше чем str, > 0 если она больше str, и 0 если строки равны. Если length больше или равен длине main_str и offset передан, substr_compare() выводит предупреждение и возвращает FALSE.

Если case_sensitivity имеет значение TRUE, сравнение выполняется с учетом регистра.

Пример 1. Пример использования substr_compare()

<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning
?>



add a note add a note User Contributed Notes
substr_compare
sleek
23-May-2005 05: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: Sat, 27 Jan 2007
 
 
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571