Balancer> Теперь — варианты на проверку окончания строки:
Balancer> 0.71
function endsWith_sub($string, $char)
function endsWith_re($haystack, $needle)
- case не задаётся, если добавить его то у меня endsWith_sub чуть чуть медленнее, чем endsWith_cmp1 становится
$case=false
endsWith_cmp1
0.30
endsWith_re
0.32
endsWith_sub
0.22
$case=true
endsWith_cmp1
0.31
endsWith_re
0.33
endsWith_sub
0.33
code text
function endsWith_sub($string, $char, $case=true)
{
$length = strlen($char);
if($case)
return (strtolower(substr($string, -$length, $length)) === strtolower($char));
return (substr($string, -$length, $length) === $char);
}
function endsWith_re($haystack, $needle, $case=true)
{
return preg_match("/".preg_quote($needle) .'$/'.($case?'i':''), $haystack);
}
может чего накосячил.
Ага, утром глянул на свежую голову: наоборот должно быть
code text
function endsWith_sub($string, $char, $case=true)
{
$length = strlen($char);
if($case)
return (substr($string, -$length, $length) === $char);
return (strtolower(substr($string, -$length, $length)) === strtolower($char));
}
function endsWith_re($haystack, $needle, $case=true)
{
if($case)
return preg_match('/'.preg_quote($needle) .'$/', $haystack);
return preg_match('/'.preg_quote($needle) .'$/i', $haystack);
}
впрочем смысл такой-же.