Home  >  Article  >  Backend Development  >  PHP diary (1) String operation php split string php string merge php string change

PHP diary (1) String operation php split string php string merge php string change

WBOY
WBOYOriginal
2016-07-29 08:49:081159browse

 1. Formatting of strings

1. String processing: trim(), ltrim(), rtrim()

$name=trim($_POST['name']);
$name=trim($_POST['password']);
$name=trim($_POST['email']);

trim() function can remove the spaces at the beginning and end of the string, And return the result string. By default, the characters removed are line feed and carriage return characters (n and r), horizontal and vertical tab characters (t and X0B). The ltrim() function only starts from the beginning of the character ( Left) Remove spaces

rtrim() function only removes spaces from the end of the function (right)

2. Format the string for display

Use HTML formatting: n12br() function

in Insert newline character before newline(n) in string

<span style="white-space:pre">	</span><pre name="code" class="html"><?php
echo nl2br("One line.\nAnother line.");
?>
result

One line.
Another line.

②Format string for print output


printf() structure

$s="world");
printf("Hello %s",$s);
3. Change the case of letters in the string


Functionstrtoupper()strtolower()ucfirst()ucwords()
Description Use $subject=Hello World Return value
Convert the string to uppercase strtoupper($subject) HELLO WORLD
Convert the string to lowercase strtolower($sub ject ) hello world
If the first character of the string is a character, convert it to uppercase ucfirst($subject) Hello world
will Capitalize the first letter of each word in the string ucwords($subject) Hello World


---------------- ------------------------------------------------

Not finished

The above introduces PHP diary (1) string operations, including PHP and string content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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