Home Backend Development PHP Tutorial Chapter 4 Data Processing-PHP String Processing-Zheng Aqi (continued)_PHP Tutorial

Chapter 4 Data Processing-PHP String Processing-Zheng Aqi (continued)_PHP Tutorial

Jul 21, 2016 pm 03:26 PM
echo and and deal with string definition data processing show logo of pass

1.字符串的定义与显示
定义:通过””,''来标志
显示:echo()和print(),但print()具有返回值值,1,而echo()没有,但echo比print()要快,print()能用在复合语句中。
2.字符串的格式化
printf(string $format[,mixed$args])
第一参数是格式字符串,$args是要替换进来的值,prinf(“%d”,$num);
说明,如果想打印一个”%”,必须用”%”,浮点数f,八进制用”0”
3.常用的字符串函数
1.计算字符串的长度
strlen(string $string),说明,1个英文长度1个字符,1个汉字长度为2个字符,空格也算一个字符。
2.将字符串改变大小写
转为小写:strtolower()
转为大写:strtoupper()
将第一个字符大写: ucfirst()
将每个单词的第一个字母大写 ucwords()
3.字符串裁剪。
当一个字符串的首尾有多余的空白字符,如空格、制表符等可以用
string trim(string $str[,string $charlist])
string rtrim(string $str[sring $charlist])
string itrim(string $str[,string $charlist])
表4.1 trim、itrim、rtrim函数的默认删除字符

字 符

ASCII码

意 义

" "

32(0x20)

空格

"\t"

9(0x09)

制表符

"\n"

10(0x)

换行

"\r"

13(0x0D)

回车

"\0"

0(0x00)

空字节

"\x0B"

11(0x0B)

垂直制表符


4. Searching for strings
string strstr(string $a, string $b)
Explanation: The strstr() function is used to find the position where the string pointer $b appears in the string $a.
And returns the string starting from $b to the end of $a string in $a string.
If there is no return value, that is, $b is not found, then FALSE is returned. The strstr() function also has a function of the same name, strchr().
5. Strings and ASCII codes
4. Comparison of strings
The comparison functions include
strcmp() //case sensitive
strcasecmp()// Insensitive to case
strncmp() //Comparison part
strncasecmp()//Insensitive to case, comparison part
5. String replacement
str_replace(search ,replace,subject)
Instructions to use the new string replace to replace the search string in the string subject
$str="I love you";
$replace=" lucy";
$end=str_replace("you",$replace,$str);
echo $end; //Output "I love lucy"
?>
is case sensitive , many-to-one and many-to-many replacements can also be achieved, but one-to-many replacement cannot be achieved.
$str="What Is Your Name";
$array=array("a","o","A","O","e");
echo str_replace($array, "",$str); //Many-to-one replacement, output "Wht Is Yur Nm"
$array1=array("a","b","c") ;
$array2=array("d","e","f");
echo str_replace($array1,$array2, "abcdef"); //Many-to-many replacement, output "defdef "
?>
substr_replace
Replace part of the string.
6. Strings and HTML
Abbreviated
7. Other string functions
1. Strings and arrays
a. Convert strings to arrays
The explode() function can split another string with the specified string and return an array
$str="Use spaces to split the string";
array=explode(" " , $str);
pint_r($array);
Output Array ( [0] => use [1] => space [2] => split [3] => string)
?>
b. Convert array to string
implode(string $glue,array $pieces)
$pieces is an array to save the string to be connected, $glue is used for connection String concatenation character. For example:
$array=array("hello","how","are","you");
$str=implode(",",$array); //Use commas as connectors
echo $str; //Output "hello,how,are,you"
?>
c. String encryption function
md5(); crypt (), but once this function is encrypted, it cannot be converted to its original form.
4.3 Example Guestbook Content Processing
A guestbook with email addresses and user messages. To extract the customer's email address and messages, it is required that the email address cannot be preceded by a "." or a comma" before the @ symbol. ,".
Set the content before the @ symbol in the email address as the user's username, and change the first person "I" in the user's message to "I".
Copy code The code is as follows:

Your email address:



Your message:







if(isset($_POST ['bt1']))
{
$Email=$_POST['Email']; //Receive Eamil address
$note=$_POST['note']; //Receive message
if(!$Email||!$note) //Determine whether the value is obtained
echo "<script>alert('Please fill in the email address and message completely!')</script>";
else
{
$array=explode("@", $Email); //Split the email address
if(count($array)!=2) //If there are two @ symbols, an error will be reported
echo "<script>alert('Email address format error!')</script>";
else
{
$username=$array[0]; //Get @ Content before the symbol
$netname=$array[1]; //Get the content after the @ symbol
//If username contains "." or "," an error will be reported
if(strstr($ username,".") or strstr($username,","))
echo "<script>alert('Email address format is wrong! ')</script>";
else
{
$str1= htmlspecialchars("<"); //Output symbol "<"
$str2= htmlspecialchars("> "); //Output the symbol ">"
//Replace "I" in the message with "I"
$newnote=str_replace("I","I",$note);
echo "";
echo "User". $str1. $username . $str2. "Hello! ";
echo "You are". $ netname. "Netizen!
";
echo "
Your message is:
".$newnote."
";
echo "}
}
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323892.htmlTechArticle1. String definition and display definition: Use "",'' to mark display: echo() and print(), but print() has a return value of 1, while echo() does not, but echo is faster than print(), print() can be used in complex...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Introduction to PHP interfaces and how to define them Introduction to PHP interfaces and how to define them Mar 23, 2024 am 09:00 AM

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

How does Golang improve data processing efficiency? How does Golang improve data processing efficiency? May 08, 2024 pm 06:03 PM

Golang improves data processing efficiency through concurrency, efficient memory management, native data structures and rich third-party libraries. Specific advantages include: Parallel processing: Coroutines support the execution of multiple tasks at the same time. Efficient memory management: The garbage collection mechanism automatically manages memory. Efficient data structures: Data structures such as slices, maps, and channels quickly access and process data. Third-party libraries: covering various data processing libraries such as fasthttp and x/text.

How do the data processing capabilities in Laravel and CodeIgniter compare? How do the data processing capabilities in Laravel and CodeIgniter compare? Jun 01, 2024 pm 01:34 PM

Compare the data processing capabilities of Laravel and CodeIgniter: ORM: Laravel uses EloquentORM, which provides class-object relational mapping, while CodeIgniter uses ActiveRecord to represent the database model as a subclass of PHP classes. Query builder: Laravel has a flexible chained query API, while CodeIgniter’s query builder is simpler and array-based. Data validation: Laravel provides a Validator class that supports custom validation rules, while CodeIgniter has less built-in validation functions and requires manual coding of custom rules. Practical case: User registration example shows Lar

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

See all articles