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

Source: Internet
Author: User
Tags php regular expression
An example of converting the hump string of a PHP interview question to an underline style. For example, if the hump string of a PHP interview question is converted into an underline style, when you see this problem, you can use ASCII code to handle it, I didn't want to use the omnipotent regular expression to convert the hump string of the PHP interview question to an underline style.

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:
$ 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 ).'
';

Answer 2:

The code is as follows:
Echo strtolower (preg_replace ('/((? <= [A-z]) (? = [A-Z])/',' _ ', $ str )).'
';

Then, in turn, how can I convert the underlined string into a camel (www.111cn.net) peak string?

The code is as follows:
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 ()
}
}

From: http://www.111cn.net/phper/php-cy/59093.htm


How can we convert string styles from camper to underline in java?

Public class transform {public static String trans (String str) {List record = new ArrayList (); for (int I = 0; I = 'A') {record. add (I); // records the location of each uppercase letter} record. remove (0); // The first one does not need to be underlined str = str. toLowerCase (); char [] charofstr = str. toCharArray (); String [] t = new String [record. size ()]; for (int I = 0; I

Php regular expression how to convert all spaces in a string into underscores

The simple code is as follows:
Preg_replace ('/[\ s]/', '_', $ content); // replace spaces, including spaces. if you have any questions, contact us ~


When the hacker saw this problem, he thought of using ASCII code to handle it. he didn't think about the omnipotent regular expression...

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.