Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-29 08:49:08
Original
1175 people have browsed it

Copy after login
1. Formatting of strings

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

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

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.");
?>
Copy after login
result

One line.
Another line.
Copy after login

②Format string for print output


printf() structure

$s="world");
printf("Hello %s",$s);
Copy after login
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.

Related labels:
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