Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Information formatting Operation in PHP

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "the method of information formatting operation in PHP". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "the method of information formatting operation in PHP" can help you solve the problem.

Formatting

/ / format $fmt = new MessageFormatter ("zh_CN", "{0grad number number integer} monkeys on {1 # number number} trees, each tree has {2 # number} monkeys"); echo $fmt- > format ([4560, 123, 4560 / 123]), PHP_EOL / 4560 monkeys in 4560 trees, each tree has 37.073 monkeys $fmt = new MessageFormatter ("de", "{0dre numbering integer} Affen auf {1 heroic numbering integer} B ä umen sind {2magistral number} Affen pro Baum"); echo $fmt- > format ([4560, 123, 4560 / 123]), PHP_EOL / 4.560 Affen auf 123B ä umen sind 37073 Affen pro Baumecho MessageFormatter::formatMessage ("zh_CN", "{0rec numberjigme integer} monkeys on {1GND numbered integer} trees, there are {2m number} monkeys on each tree", [4560, 123, 4560 / 123]), PHP_EOL / 4560 monkeys in 4560 trees, there are 37.073 monkeys in each tree echo MessageFormatter::formatMessage ("de", "{0 Affen auf numberlast integer} Affen auf {1 # numbered number} integer B ä umen sind {2 # number} Affen pro Baum", [4560, 123, 4560 / 123]), PHP_EOL;// 4.560 Affen auf 123 B ä umen sind 37073 Affen pro Baum

You see that? A placeholder similar to a precompiled operation in PDO. After calling the format () method, you can have the parameters in this method replace the contents of the placeholder. We can specify the parameter type and position used by the placeholder, {parameter subscript, type, extension type} this is the rule definition of the placeholder formatted by this information data. It seems very simple, but in fact it has more functions, which we will see later. It is important to note, however, that it only supports numbers, dates, and text fragment types, and the official documentation is available in the reference link at the end of the article.

The static method MessageFormatter::formatMessage () can specify the language, pre-operation statements, and substitution parameters at once, without the need to instantiate before calling the format () method.

Unformatting (getting an array of parameters according to rules)

Can be formatted, of course, we can also anti-format the relevant strings according to the statement rules to get the parameter list of the corresponding placeholders.

/ / according to the formatting rules, get the rule parameters $fmt = new MessageFormatter ("zh_CN", "{0grad numbering integer} monkeys on {1 # numbered integer} trees, each tree has {2 # number} monkeys"); $res = $fmt- > parse ("4560 monkeys in the 123rd tree, 37.073 monkeys in each tree"); var_export ($res); / / falseecho "ERROR:". $fmt- > getErrorMessage (). (). $fmt- > getErrorCode (). ")"; / / ERROR: Parsing failed: U_MESSAGE_PARSE_ERROR (6) $fmt = new MessageFormatter ("en_US", "{0 monkeys on number integer} monkeys on {1 # number number} integer} trees make {2 # number} monkeys per tree"); $res = $fmt- > parse ("4560 monkeys on 123 trees make 37.073 monkeys per tree"); var_export ($res) / / array (/ / 0 = > 4560 parse / 1 = > 123 Affen auf / 2 = > 37.073 umen sind /) $fmt = new MessageFormatter ("de", "{0ME numbering} Affen auf {1D numbering} B ä umen sind {2cr number} Affen pro Baum"); $res = $fmt- > parse ("4.560 Affen auf 123B ä umen sind 37073 Affen pro Baum"); var_export ($res) / / array (/ / 0 = > 4560 Affen pro Baum / 1 = > 123 Affen auf / 2 = > 37.073 /) $fmt = MessageFormatter::parseMessage ("de", "{0nd numbering integer} Affen auf {1m numbering} B ä Affen pro Baum", "4.560 Affen auf 123B ä umen sind 37073 Affen pro Baum"); var_export ($fmt) / / array (/ / 0 = > 4560 penny / 1 = > 123 pencil / 2 = > 37.073 /)

This can be done using the instantiated parse () method or directly using the static method MessageFormatter::parseMessage (). It is important to note that this operation can be problematic for zh_CN, that is, the Chinese language locale. You can see the error message and the error code through getErrorMessage () and getErrorCode (), and you can see that for Chinese, the error message returned directly is parsing failure.

Set acquisition rules

In the instantiated object, we can also modify the rule statement dynamically.

/ / set the acquisition rule $fmt = new MessageFormatter ("zh_CN", "{0, number} monkey on {1, number} tree"); echo "default rule:". $fmt- > getPattern (), PHP_EOL; / / default rule: "{0, number} monkey" echo "format result on {1, number} tree:". $fmt- > format (array (123,456)), PHP_EOL; / / format result: $fmt- > setPattern on 456 trees ("{0, number} trees have {1, number} monkeys"); echo "New Rule:". $fmt- > getPattern (), PHP_EOL; / / New rule: "{0, number} tree has {1, number} monkey" echo "new rule format result:". $fmt- > format (array (123,456)), PHP_EOL; / / New rule format result: there are 456 monkeys on 123 trees

There are two very simple methods, setPattern () to set the formatting rules corresponding to the current instantiation, and getPattern () to get the formatting rules for viewing the currently instantiated objects. After the new rule is set, format () or parse () is executed according to the new rule statement.

Format complete example

As mentioned above, in addition to numbers, there can also be placeholders in date format, which we will demonstrate.

Echo MessageFormatter::formatMessage ("zh_CN", "Today is {3, date, full}, the current time is {3, time,:: Hms}, I'm going to start {0}, today I'm going to meet with {2 date numbered integer} people, and don't forget to pay the electricity bill of {1 # date numbered currency} yuan", ["go to work", 35.33,25, new DateTime ()], PHP_EOL / / Today is Monday, November 16, 2020. The current time is 10:09:30. I have to get ready to start work. I have to meet 25 people today, and I can't forget to pay the electricity bill of RMB35.33.

In this statement, the order of the parameters given is not in the order in which the placeholders appear in the statement, so it does not matter, just specify the parameter array subscript of the corresponding position, for example, the first {3, date, full} specifies the fourth element in the parameter array (starting with 0). Date type and time type are all types that can be specified, of course, we can also specify their date format, such as the second placeholder, we only display the current time, minute and second information.

If it is a string information, then all you need is a simple {0}, and the string does not require too many type settings. Numeric types, on the other hand, can be directly formatted into types such as currency, just like those that can be specified in the NumberFormatter we talked about earlier.

After reading this example, do you feel the power of this MessageFormatter? Don't worry, it has a better ability.

Plural display according to the content of the parameter

For plural, there is no such sentence in Chinese grammar. For example, one cat is a cat and two cats are two cats.

Echo MessageFormatter::formatMessage ("en_US", "I Have {0, plural, = 0 {no cat} = 1 {a cat} other {# cats}", [0]), PHP_EOL; / / I Have no catecho MessageFormatter::formatMessage ("en_US", "I Have {0, plural, = 0 {no cat} = 1 {a cat} other {# cats}", [1]), PHP_EOL / I Have a catecho MessageFormatter::formatMessage ("en_US", "I Have {0, plural, = 0 {no cat} = 1 {a cat} other {# cats}}", [2]), PHP_EOL; / / I Have 2 cats

Although the plural of the parameter type means plural, we can actually think of it as the use of a switch () statement.

Echo MessageFormatter::formatMessage ("zh_CN", "I {0, plural, = 0 {no cats} other {# cats}}", [0]), PHP_EOL; / / I do not have a cat echo MessageFormatter::formatMessage ("zh_CN", "I {0, plural, = 0 {no cats} other {# cats}", [1]), PHP_EOL / / I have 1 cat echo MessageFormatter::formatMessage ("zh_CN", "I {0, plural, = 0 {no cats} other {# cats}}", [2]), PHP_EOL; / / I have 2 cats

The # sign is the original content of the corresponding parameter value, and this set of syntax takes the MessageFormatter class to a higher level. Let's take a look at this question first:

Echo MessageFormatter::formatMessage ("en_US", "I Have {0, plural, = 0 {no cat} = 1 {a cat} other {# cats}}", [- 1]), PHP_EOL; / / I Have-1 cats

The parameter is wrong,-1 cat is wrong, it doesn't matter, there are other ways to solve this problem.

Select a conditional rule

/ / Select the expression echo MessageFormatter::formatMessage ("en_US", "I Have {0, choice, 0 # no cats | 1 # one cat | 2 # {0, number} cats}", [- 1]), PHP_EOL; / / I Have no catsecho MessageFormatter::formatMessage ("en_US", "I Have {0, choice, 0 # no cats | 1 # one cat | 2 # {0, number} cats}", [0]), PHP_EOL / I Have no catsecho MessageFormatter::formatMessage ("en_US", "I Have {0, choice, 0 # no cats | 1 # one cat | 2 # {0, number} cats}", [1]), PHP_EOL; / / I Have one catecho MessageFormatter::formatMessage ("en_US", "I Have {0, choice, 0 # no cats | 1 # one cat | 2 # {0, number} cats}", [2]), PHP_EOL / / I Have 2 catsecho MessageFormatter::formatMessage ("en_US", "I Have {0, choice, 0 # no cats | 1 # one cat | 2 # {0, number} cats}", [10]), PHP_EOL; / / I Have 10 cats

As you can see from the word choice, this is a choice-related grammar. The latter parameter is actually an interval that represents which content is used in the range of = 2. In addition, a placeholder rule can continue to cover placeholders.

This is the end of the content about "the method of information formatting operation in PHP". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report