Home Backend Development PHP Tutorial 第4章 数据处理-php字符串的处理-郑阿奇(续)_PHP

第4章 数据处理-php字符串的处理-郑阿奇(续)_PHP

Jun 01, 2016 pm 12:16 PM
string

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.字符串的查找
string strstr(string $a, string $b)
说明:strstr()函数用于查找字符串指针$b在字符串$a中出现的位置,
并返回$a字符串中从$b开始到$a字符串结束处的字符串。
如果没有返回值,即没有发现$b,则返回FALSE。strstr()函数还有一个同名函数strchr()。
5.字符串与ASCII码
4.字符串的比较
比较函数有
strcmp() //区分大小写
strcasecmp()//不区分大小写
strncmp() //比较部分
strncasecmp()//不区分大小写,比较部分
5.字符串的替换
str_replace(search,replace,subject)
说明使用新的字符串replace替换字符串subject中的search字符串
$str="I love you";
$replace="lucy";
$end=str_replace("you",$replace,$str);
echo $end; //输出"I love lucy"
?>
对大小写敏感,还可实现多对一、多对多的替换,但无法实现一对多的替换。
$str="What Is Your Name";
$array=array("a","o","A","O","e");
echo str_replace($array, "",$str); //多对一的替换,输出"Wht Is Yur Nm"
$array1=array("a","b","c");
$array2=array("d","e","f");
echo str_replace($array1,$array2, "abcdef"); //多对多的替换,输出"defdef"
?>
substr_replace
替换字符串的一部分。
6.字符串与HTML

7.其它字符串函数
1.字符串与数组
a.字符串转化为数组
explode()函数可以用指定的字符串分割另一个字符串,并返回一个数组
$str="使用 空格 分割 字符串";
array=explode(" ", $str);
pint_r($array);
输出Array ( [0] => 使用 [1] => 空格 [2] => 分割 [3] => 字符串 )
?>
b.数组转化为字符串
implode(string $glue,array $pieces)
$pieces是保存要连接的字符串的数组,$glue是用于连接字符串的连接符。例如:
$array=array("hello","how","are","you");
$str=implode(",",$array); //使用逗号作为连接符
echo $str; //输出"hello,how,are,you"
?>
c.字符串的加密函数
md5(); crypt(),但这个函数一旦加密后就无法转化为原来的形式。
4.3实例留言薄内容处理
一个留言簿,留言簿上有Email地址和用户的留言,提取客户的Email地址和留言,要求Email地址中@符号前不能有点“.”或逗号“,”。
将Email地址中@符号前的内容作为用户的用户名,并将用户留言中第一人称“我”修改为“本人”。
复制代码 代码如下:

您的Email地址:



您的留言:







if(isset($_POST['bt1']))
{
$Email=$_POST['Email']; //接收Eamil地址
$note=$_POST['note']; //接收留言
if(!$Email||!$note) //判断是否取得值
echo "<script>alert('Email地址和留言请填写完整!')</script>";
else
{
$array=explode("@", $Email); //分割Email地址
if(count($array)!=2) //如果有两个@符号则报错
echo "<script>alert('Email地址格式错误!')</script>";
else
{
$username=$array[0]; //取得@符号前的内容
$netname=$array[1]; //取得@符号后的内容
//如果username中含有“.”或“,”则报错
if(strstr($username,".") or strstr($username,","))
echo "<script>alert('Email地址格式错误!')</script>";
else
{
$str1= htmlspecialchars("$str2= htmlspecialchars(">"); //输出符号“>”
//将留言中的“我”用“本人”替代
$newnote=str_replace("我","本人",$note);
echo "";
echo "用户". $str1. $username . $str2. "您好! ";
echo "您是". $netname. "网友!
";
echo "
您的留言是:
    ".$newnote."
";
echo "
";
}
}
}
}
?>
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

Video Face Swap

Video Face Swap

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

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)

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

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 intercept a string in Go language How to intercept a string in Go language Mar 13, 2024 am 08:33 AM

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

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.

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

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