PHP Learning String Courseware_PHP Tutorial
Syntax
String string can be defined in three ways
' '(single quote) "" (double quote) <<<(delimiter)
Escape sequence Can be interpreted as
The biggest feature of double quotes is that the variable name will be replaced by the variable value
delimiter can be output as is in html format
Print string There are two methods
echo. It is a language structure, not a real function. The difference between it and print is that it can accept multiple parameters.
print syntax bool (Boolean line) print(string agr) secondary function output string if Returns 1 on success and 0 on failure. For example, if the customer's browser suddenly hangs during transmission, it will cause a failure
String processing function
ltrim Syntax format: string ltrim(string str); Delete characters Leading spaces in the string
rtrim Grammar format: string rtrim(string str); Delete the suffix spaces in the string
trim Grammar format: string rtim(string str); Delete the spaces at both ends of the grandfather string
strrev Grammar Format: string strrev(string str); Reverse the string Reverse the string front and back
strtolower Syntax format: string strtolower (string str); Change all strings to lowercase
strtoupper Syntax 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); The first letter of each word is changed to uppercase
Format 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 outputs a formatted string and returns an integer
Syntax format: int printf(string format, mixed [args]...);
Example
printf("%d--%s- -%x",72,72,72);
Output result: 72--72--48
Split and spell string
explode syntax format: array explode ( string separator, string string [, int limit] )
implode syntax format: string implode(shring glue, array pieces) concatenates 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 sub-string is used to compare the size. Negative number means 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 length of the string and return the 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 start to stop If stop is omitted, go to the end. If the start is set to a negative number, it means taking
strstr string strstr(string haystack, string needle); returns the beginning of a certain string in the string to the end of the string, that is, starting from somewhere Get the last segment of the string
strrchr for the boundary point. Syntax format: string strrchr(string haystack, string needle); Get the last position of the string. Intercept the string from back to front. If not found, return false.
strpos Syntax format: int strpos(string haystack, string needle, int [offset]); Searching for the first occurrence of the string starts from 0 by default. If not found, return false
strrpos Syntax format: int strrpos(string haystack, char needle); Find the last occurrence of the string. Return false if not found. Return value integer
String padding
str_pad syntax format Can be used to protect some sensitive information
str_pad($input, 10 ); , STR_PAD_BOTH); // produces "__Alien___"
str_pad($input, 6 , "___"); // produces "Alien_"
STR_PAD_LEFT String left padding
STR_PAD_RIGHT String right padded
STR_PAD_BOTH Adding both ends of string
Replacement of string
str_replace() There are three methods of function replacement
str_replace("l","@","hello"); // What to replace What to replace Who to replace
$arr2=array("%color%","%title%","%body%");
$arr3=array("red","hello", "world");
echo str_replace($arr2,$arr3,"%body%");
Touch version Method
String translation
strtr() is an important replacement function 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);
Just translate something into something
strtr(string $str, array $replace_pairs)
html processing
HTML entity processing
htmlentities(); (specify conversion mode and characters Set encoding)
ENT_COMPAT (default): Specifies to convert double quotes but retain single quotes
ENT_QUOTES: Indicates that double quotes and single quotes are converted at the same time
ENT_NOQUOTES: Indicates that neither is converted
htmlspecialchars(); (Convert special symbols such as <> into entities<>)
Syntax format
$str="
China
";echo htmlentities($str); //Convert encoding
echo htmlentities($str,ENT_QUOTES,"EUC-JP");//Convert single and double quotes, followed by the encoding of the word
Processing of quotation marks
stripslashes(); (is the inverse function of addslashes, showing the transferred characters)
addslashes(); (put ' ” NULL(Processing URLs mainly includes:
Parsing of URL strings
parse_str(); (parse the request string and convert it into an array)
parse_url(); (parse the complete URL and convert it into an array)
URL Encoding processing
rawurlencode(); (replace all non-alphanumeric characters into % followed by two hexadecimal digits)
urlencode(); (replace all non-alphanumeric characters into % followed by Two-digit hexadecimal, spaces become + signs)
rawurldecode(); (parse and restore %-encoded URLs)
urldecode(); (parse and restore %##-encoded URLs)
Construct query strings, etc.
http_build_query(); (Generate the request string after url-encoded)


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
