开发者

How can I test whether a single character is lower case in PHP?

开发者 https://www.devze.com 2023-02-15 21:29 出处:网络
I want to implement a small routine that generates the plural form of the name of an entity in Spanish. Basically, it takes a WordInCSharpCase or a wordInJavaCase, such as MedioDePago (payment method)

I want to implement a small routine that generates the plural form of the name of an entity in Spanish. Basically, it takes a WordInCSharpCase or a wordInJavaCase, such as MedioDePago (payment method), and it appends an "s" at right before the first non-lowercase character (excluding the first character), which in this case would produce the string MediosDePago 开发者_如何转开发(payment methods). In C#, this routine was:

public string Pluralize(string input)
{
    int i = 0;
    while (++i < input.Length)
        if (!char.IsLower(input[i]))
            break;

    StringBuilder builder = new StringBuilder(input);
    builder.Insert(i, 's');
    return builder.ToString();
}

Now I need to implement this routine in PHP, but I cannot find an equivalent of C#'s char.IsLower. The only thing I have found is ctype_lower, but it takes a string as an input, and creating/testing/discarding several strings would be too inefficient. Does PHP have a function that tests whether a single character is in lower case?


Well, first off, creating several strings is not inefficient. A string is a native type in PHP and it's quite efficient at doing it. What I would do is something like this:

$callback = function($match) {
    return $match[1] . 's' . $match[2];
};

$string = preg_replace_callback('/^([A-Za-z][a-z]*)([A-Z]|$)/', $callback, $string);

Or, as @Kevin suggests in the comments:

$string = preg_replace('/^([A-Za-z][a-z]*)([A-Z]|$)/', '\1s\2', $string);

It's more efficient since it doesn't need to capture past the first non-lowercase character.


There is no difference between a string and a char so using ctype_lower() is absolutely OK. In fact there isn't even a real string in PHP, a string in PHP is merely a blob of binary data.

I actually don't know if ctype_lower() can deal with extended character sets and characters outside the ASCII range. If that's a requirement, you must perhaps use the PCRE unicode capabilities to check fo lower-case characters outside the ASCII range.


You can use mb_strtolower for this, like so:

$a = 'a';
var_dump(mb_strtolower($a, "UTF-8") == $a); // bool(true)

$b = 'A';
var_dump(mb_strtolower($b, "UTF-8") == $b); // bool(false)
0

精彩评论

暂无评论...
验证码 换一张
取 消