Home > Backend Development > PHP Tutorial > String Array Chapter 4 Data Processing-PHP String Processing-Zheng Aqi Continued

String Array Chapter 4 Data Processing-PHP String Processing-Zheng Aqi Continued

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:46:00
Original
1045 people have browsed it

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 $a character The string starting from $b to the end of $a string in the 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
Comparison functions include
strcmp() //Case-sensitive
strcasecmp() //Case-insensitive
strncmp() //Comparison part
strncasecmp() //Case-insensitive, comparison part
5. String replacement
str_replace(search,replace,subject)
Instructions to use 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"
?>
It is case-sensitive and can also achieve many-to-one and many-to-many replacements, but it cannot achieve one-to-many replacement.
$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
Original
7. Other string functions
1. Strings and arrays
a. Convert strings to arrays
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 saved An array of strings to be concatenated, $glue is the concatenator used to concatenate strings. For example:
$array=array("hello","how","are","you");
$str=implode(",",$array); //Use commas as connections Symbol
echo $str; //Output "hello,how,are,you"
?>
c. String encryption function
md5(); crypt(), but once this function is encrypted, it cannot be converted to the original form.
4.3 Example of 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 must not have a dot "." or a comma "," before the @ symbol.
Use 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 the code The code is as follows:



Your Email address:



You Message:







if(isset($_POST['bt1']))
{
$Email=$_POST['Email']; //Receive Eamil address
$note=$_POST['note']; //Receive messages
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 "";
else
{
$username=$array[0]; //Get the 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 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 "
";
}
}
}
}
?>

The above introduces the string array. Chapter 4 Data Processing - PHP String Processing - Zheng Aqi continued, including the content of string array. I hope it will be helpful to friends who are interested in PHP tutorials.

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