Example of the implementation of a camel style string in PHP converted to an underscore style string

Source: Internet
Author: User
This article mainly introduces PHP implementation of the Hump style string (first letter capitalization) into an underline style string, involving PHP regular replacement related operation skills, the need for friends can refer to the following

This example describes how PHP implements the Camel style string (capitalized) into an underline style string. Share to everyone for your reference, as follows:

1. How to convert a camel style string into an underline style string in PHP. Example: If the input is foobar, the output is Foo_bar

The following is a regular way to complete, since the use of the regular, the method must be more than one, we look at the following way


Echo strtolower (Preg_replace ('/(? <=[a-z]) ([A-z])/', ' _$1 ', ' FooBar ');//output:foo_barecho "<br>"; Echo Strtolower (Preg_replace ('/(? <=[a-z]) ([A-z])/', ' _$1 ', ' foo ');//output:fooecho "<br>"; Echo Strtolower ( Preg_replace ('/(<=[a-z]) ([A-z])/', ' _$1 ', ' Foobarb ');//output:foo_bar_becho "<br>";

Let's explain the meaning of the above. The basic knowledge of specific regular, here is limited space is not specifically introduced, the end of the article will be accompanied by a few summary of the relatively good regular expression of the article.

The above-mentioned regular is mainly used in the regular expression of the look-around boundary matching syntax. The specific definition is as follows (excerpt):

Look around the literal meaning is left and right to see, need to meet a number of conditions, in essence, it is also a matching boundary, there are some requirements for the boundary, the requirement is for the left side or the string, according to the requirements of different, divided into four kinds of surround view:

In order to look around, the syntax is (? = ...), which requires that the right string match the specified expression, such as the expression ABC (? =def), and (? =def) to the left of the character C, which matches the boundary on the right side of C, the requirement for this boundary is that it has a def such as abcdef, if not, such as ABCD, it does not match;

The negation order , the grammar is (?! ...), requires that the right string does not match the specified expression, such as the expression s (?!). ing), which matches the general S, but does not match the s after having ing;

Be sure to look in reverse , grammar is (<= ...), ask the left string to match the specified expression, such as expression (? <=\s) ABC, (? <=\s) on the left side of character A, which matches the boundary of a left side, the requirement for this boundary is, The left side of it must be a blank character;

Negative Reverse Look, the syntax is (?<!...), requires that the left string does not match the specified expression, such as the expression (? <!\w) Cat, (? <!\w) on the left side of the character C, which matches the boundary of the left side of C, the requirement for this boundary is that The left side of it cannot be a word character.

As you can see, surround look also uses parentheses (), however, it is not a grouping and does not occupy a grouping number.

Go back to the regular expression above us, the first parenthesis (? <=[a-z]), which is the syntax for affirmative-order surround, which requires a lowercase letter to the left of the matched string. The second parenthesis is a grouping that matches the uppercase letters, noting that the grouping numbers in the regular are starting from 1, which is different from our traditional programming subscript, which is usually starting from 0. The first parenthesis itself is the syntax, which does not occupy the grouping number, so the next $ $ is the content in the second parenthesis that matches, adds a _ symbol before it, and then converts the whole string to lowercase.

Now that we have been able to turn the Hump method into an underlined style, what if it should be done in turn?

2. How to convert an underline style string into a camel-style string 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; Foobarecho "<br>"; $str = Preg_replace_callback ('/_+ ([A-z])/', function ($matches) {  return Strtoupper ($ MATCHES[1]), "foo"); Echo $str; Fooecho "<br>"; $str = Preg_replace_callback ('/_+ ([A-z])/', function ($matches) {  return Strtoupper ($ MATCHES[1]), "Foo_bar_b"); Echo $str; Foobarbecho "<br>";

Here we use a preg_replace_callback function that performs a regular expression search and replaces it with a callback. In other words, the first argument is a regular expression, the second argument is a callback function that matches the result, and the third argument is the string that needs to be matched. Note that when a callback function is called, it is called every time it is matched to the result, not just once, but not by the match. and the parameter of the callback function is the result of a match, is a complete match, is a complete match, is the match of the matches[0] matches[1] first capturing subgroup, and so on. and the callback function needs to return the result of the change, otherwise it ignores the captured string

Regular expressions are relatively simple and are not specifically analyzed here.

Related Article

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.