Home > Backend Development > PHP Tutorial > PHP interview question: Example of converting camelCase string to underscore style, CamelCase Underline_PHP Tutorial

PHP interview question: Example of converting camelCase string to underscore style, CamelCase Underline_PHP Tutorial

WBOY
Release: 2016-07-13 10:18:16
Original
915 people have browsed it

An example of converting a camel case string into an underline style for PHP interview questions, camel case underline

When I saw this question, I thought of using ASCII code to process it, but I didn’t go into it. Think about the omnipotent rules. Well, let’s take a look at the answer:

Answer 1:

The code is as follows Copy the 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) . '
';

Answer 2:

Copy the code as follows
echo strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $str)).'< br>';

How to reversely divide the string by underline and convert it into a camel (www.111cn.net) peaked string

Copy the code 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 to use java to convert the string style from camel case to underline form?

public class transform {public static String trans(String str){List record =new ArrayList();for(int i=0;i='A')){record.add(i);//Record the position of each uppercase letter}}record.remove(0);//The first one No need to add underline str= str.toLowerCase();char[] charofstr = str.toCharArray();String[] t =new String[record.size()];for(int i=0;i

How to convert all spaces in a string into underscores with php regular expression

The very simple code is as follows:
preg_replace('/[\s ]/', '_', $content); // Replace spaces, including large spaces. If you have any questions, please feel free to ask~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/884775.htmlTechArticleAn example of converting a camel case string into an underscore style for PHP interview questions. When I saw this question, I saw the camel case underline style. What I thought of was to use ASCII codes for processing, and I didn’t think about the universal regular expression...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template