Example of converting a hump string into an underline style in a PHP interview question

Source: Internet
Author: User
Tags ord regular expression

When I saw this problem, I thought of using ASCII code to handle it. I didn't think about it as a universal regular expression. Okay, let's take a look at the answer:

Answer 1:

The code is as follows: Copy code

$ Str = 'openapi ';

$ Length = mb_strlen ($ str );

$ New = '';

For ($ I = 0; $ I <$ length; $ I ++)
{
$ Num = ord ($ str [$ I]);
$ Pre = ord ($ str [$ I-1]);

$ New. = ($ I! = 0 & ($ num >=65 & $ num <= 90) & ($ pre >=97 & $ pre <= 122 ))? "_ {$ Str [$ I]}": $ str [$ I];
} Www.111cn.net

Echo strtolower ($ new). '<br> ';

Answer 2:

The code is as follows: Copy code

Echo strtolower (preg_replace ('/((? <= [A-z]) (? = [A-Z])/',' _ ', $ str).' <br> ';

In turn, how does one convert an underscore to a camper string?

The code is as follows: Copy code

F = new File ("d:/temp/t.txt ")
If (f. exists ()){
F. eachLine {line->
Line = line. trim ()
String [] elems = line. split ('_')
For (int I = 0; I <elems. length; I ++ ){
Elems [I] = elems [I]. toLowerCase ()
If (I! = 0 ){
String elem = elems [I]
Char first = elem [0] as char
Elems [I] = "" + (char) (first-32) + elem. substring (1)
            }
        }
Println elems. join ()
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.