This article illustrates the method of converting camel-style strings (first letters) into underlined strings in PHP. Share it for your reference, as follows:
1. How to convert camel-style strings into underlined strings in php. Example: If the input is FooBar, the output is foo_bar
The following is done in a regular way. Since the rules are used, there are definitely more than one method. Let’s take a look at the following method.
echo strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', 'fooBar')); //output:foo_bar echo "<br>"; echo strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', 'foo')); //output:foo echo "<br>"; echo strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', 'fooBarB')); //output:foo_bar_b echo "<br>";
Let’s explain the meaning of the above regularity. The basic knowledge of specific rules is not introduced in detail if there is limited space here. At the end of the article, several articles with better regular expressions summarized will be accompanied by.
The above rules mainly use the regular expressionsLooking around the boundary matchingsyntax. The specific definition is as follows (excerpt):
The literal meaning of surround viewing is to look left and right. It needs to meet some conditions. In essence, it also matches the boundary and has some requirements for the boundary. This requirement is for the string on the left or right. According to the requirements, it is divided into four types of surround viewing:
A positive order of circumcision, the syntax is (?=...), which requires the string on the right to match the specified expression, such as the expression abc(?=def), (?=def) on the right side of the character c, that is, to match the boundary on the right side of c. The requirement for this boundary is that there is a def on the right side of it, such as abcdef. If there is no, such as abcd, it does not match;
Negative order around view, the syntax is (?!...), which requires that the string on the right cannot match the specified expression, such as the expression s(?!ing), which matches the general s, but does not match the s followed by ing;
Affirmative reverse order circular viewing, the syntax is (?<=...), which requires the string on the left to match the specified expression, such as the expression (?<=\s)abc, (?<=\s) is on the left side of character a, that is, match the boundary on the left side of a. The requirement for this boundary is that it must have a blank character on the left;
Negative reverse order circular viewing, the syntax is (?<!...), which requires that the string on the left cannot match the specified expression, such as the expression (?<!\w)cat, (?<!\w) is on the left side of the character c, that is, to match the boundary on the left side of c. The requirement for this boundary is that the left side of it cannot be word characters.
It can be seen that surround viewing also uses brackets (), but it is not a grouping and does not occupy the grouping number.
Continue back to our regular expression above, the first bracket (?<=[a-z]), which is a syntax for positive inverse order, requiring lowercase letters to the left of the matching string. The second bracket is a grouping, matching capital letters. Note that the grouping number in the regularity starts from 1, which is different from our traditional programming subscripts generally start from 0. The first bracket itself is syntax, which does not occupy grouping numbers, so the following $1 is the content in the matching second bracket, and add a _ symbol before it, and finally convert the entire string into lowercase as a whole.
Since we can already change the camel method to an underlined style, what should we do if it is reversed?
2. How to convert underlined-style strings into camel-style strings in php. Example: If the input is foo_bar, the output is FooBar
$str = preg_replace_callback('/_+([a-z])/',function($matches){ print_r($matches); //Array ( [0] => _b [1] => b ) return strtoupper($matches[1]); },'foo_bar'); echo $str; //fooBar echo "<br>"; $str = preg_replace_callback('/_+([a-z])/',function($matches){ return strtoupper($matches[1]); },'foo'); echo $str; //foo echo "<br>"; $str = preg_replace_callback('/_+([a-z])/',function($matches){ return strtoupper($matches[1]); },'foo_bar_b'); echo $str; //fooBarB echo "<br>";
We used it herepreg_replace_callback
Function, which performs a regular expression search and replaces it with a callback. In other words, the first parameter is a regular expression, the second parameter is a callback function that matches the result, and the third parameter is a string that needs to be matched. Note that when the callback function is called, it will be called each time the result is matched, and the number of calls is not only once, and if it does not match, it will not be called. And the parameters of the callback function are the result of the match and are the complete match.matches[0]
It's a complete match,matches[1]
is the first to capture the match of the subgroup, and so on. And the callback function needs to return the changed result, otherwise the captured string is ignored.
Regular expressions are relatively simple, so I won't analyze them specifically here.
Regular quick lookup table https:///article/
For detailed regular expression syntax, please refer to:
30-minute tutorial on getting started with regular expressions https:///tools/
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools./regex/javascript
Regular expression online generation tool:
http://tools./regex/create_reg
For more information about PHP related content, please check out the topic of this site:Summary of usage of php regular expressions》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.