PHP learning string courseware

黄舟
Release: 2023-03-03 15:52:02
Original
1021 people have browsed it

Grammar
String string can be defined in three ways
' '(single quote) "" (double quote) <<<(delimiter)

The escape sequence can be interpreted as

double quote using The biggest feature is that the variable name will be replaced by the variable value

The delimiter can be output as it is in html format


There are two ways to print a string
echo It is a language structure, not a real function. The difference between it and print is that it can be accepted Multiple parameters
print Syntax bool (Boolean line) print(string agr) The function outputs a string if successful and returns 1 if failed. For example, if the customer's browser suddenly hangs during transmission, it will cause a failure scenario

String Processing function
ltrim Grammar format: string ltrim(string str); Remove leading spaces from string
rtrim Grammar format: string rtrim(string str); Delete suffix spaces from string
trim Grammar format: string rtim(string str); Delete the spaces at both ends of the grandfather thread
strrev Grammar format: string strrev(string str); Reverse the string Turn the string upside down
strtolower Grammar format: string strtolower (string str); Change all strings into lowercase
strtoupper Grammar format :string strtoupper (string str); Change all strings to uppercase
ucfirst Syntax format: string ucfirst(string str); Change the first letter of the string to uppercase
ucworde Syntax format: string ucworde(string str); Change characters The first letter of each word in the string is changed to uppercase

Format the string for display
sprintf This function formats the string
sprintf() syntax format string sprintf(string format,mixed[args]...)
Example:
$a=sprintf ("%'*6s","kkk")
echo $a;
Output result: ***kkk
b Convert integer to binary.
c Convert the integer into the corresponding ASCII character.
d Convert integer to decimal place.
f Convert single precision numbers to floating point numbers.
o Convert an integer to octal.
s Convert to string.
x Convert integer to lower case hexadecimal.
X Convert integer to uppercase hexadecimal.
printf Output formatted string Return value integer
Syntax format: int printf(string format, mixed [args]...);
Example
printf("%d--%s--%x",72,72 ,72);
Output results: 72--72--48

Split and spell strings
explode syntax format: array explode (string separator, string string [, int limit])
implode syntax format: string implode( shring glue, array pieces) connects the array function into a string

String comparison
strcmp syntax format: int strcmp(string 1, string 2) The return value is an integer substring
strcmp substring is used to compare the size and represents a negative number 1 is less than 2. Positive number means 1 is greater than 2. 0 means equal.
strlen syntax format: int strlen(string str); get the string length and return value integer.
substr syntax format: string substr(string string, int start, int [length]) ;
Get part of the string. String is a string. Take a few characters from the start to the stop. If the stop is omitted, go to the end. If the start is set to a negative number, it means taking from the back to the front.
strstr string strstr(string haystack, string needle); Return characters From the beginning to the end of a string in a string, that is to say, taking a certain point as the boundary to get the subsequent strings
strrchr Syntax format: string strrchr(string haystack, string needle); Get the last position of the string from back to front If the intercepted string is not found, return false.
strpos Grammar format: int strpos(string haystack, string needle, int [offset]); Searching for the first position of the string starts from 0 by default. If not found, return false.
strrpos Grammar format: int strrpos(string haystack, char needle); Find the last occurrence of the string. If not found, return false. Return value integer.

String padding.

The str_pad syntax format can be used to protect some sensitive information
str_pad($input, 10); -Alien"
str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
str_pad($input, 6, "___"); // produces "Alien_"
STR_PAD_LEFT String left Add
STR_PAD_RIGHT String right append
STR_PAD_BOTH String both ends append

String replacement
str_replace() Function replacement
There are three methods
str_replace("l","@","hello"); //What to replace What to use Replace Who to replace
$arr2=array("%color%","%title%","%body%");
$arr3=array("red","hello","world");
echo str_replace ($arr2,$arr3,"%body%");
How to touch the version

String translation
strtr() an important The replacement function is also called string translation
strtr also has two calling methods
strtr(string $str, string $from, string $to)
$arr5=array("hello"=>"hi","hi"= >"hello");
echo strtr("hello is hi",$arr5);
is what is translated into something
strtr(string $str, array $replace_pairs)


html processing
HTML entity processing
htmlentities(); (Specify conversion mode and character set encoding)
ENT_COMPAT (default): Specify to convert double quotes but retain single quotes
ENT_QUOTES: Indicates converting double quotes and single quotes at the same time
ENT_NOQUOTES: Indicates neither conversion
htmlspecialchars( ); (Convert special symbols such as <> into entities<>)
Grammar format
$str="

China

";
echo htmlentities($ str); //Convert the encoding
echo htmlentities($str,ENT_QUOTES,"EUC-JP");//Convert both single and double quotes, followed by the encoding of the word
Processing of quotation marks
stripslashes(); (is the inverse of addslashes function, displays the transferred characters)
addslashes(); (put ' ” NULL(

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!