Chapter 4 data processing php string processing continued cheng aki of

  • 2020-05-09 18:19:10
  • OfStack

1. Definition and display of strings
Definition: marked by ""," "
Display :echo() and print(), but print() has the return value value, 1, echo() does not, but echo is faster than print(), and print() can be used in compound statements.
2. String formatting
printf(string $format[,mixed$args])
The first parameter is the format string,$args is the value to be replaced, prinf(" %d ",$num);
Note: if you want to print a %, you must use %, f, and 0 for base 8.
3. Common string functions
1. Calculate the length of a string
strlen(string $string) indicates that an English character is 1 character long, a Chinese character is 2 characters long, and a space is also 1 character.
2. Change the case of the string
Lowercase: strtolower()
Uppercase: strtoupper()
Capitalize the first character: ucfirst()
Capitalize the first letter of each word ucwords()
3. String clipping.
When there are extra white space characters at the beginning and end of a string, such as Spaces, tabs, etc
string trim(string $str[,string $charlist])
string rtrim(string $str[sring $charlist])
string itrim(string $str[,string $charlist])
Table 4.1 default delete characters for trim, itrim, rtrim functions

Word operator

ASCII code

means

" "

32(0x20)

The blank space

"\t"

9(0x09)

tabs

"\n"

10(0x)

A newline

"\r"

13(0x0D)

enter

"\0"

0(0x00)

Null bytes

"\x0B"

11(0x0B)

Vertical TAB


4. Search for strings
string strstr(string $a, string $b)
Note: the strstr() function is used to find the position of the string pointer $b in the string $a,
And returns the string in the $a string from the beginning of $b to the end of the $a string.
If there is no return value, $b is not found, then FALSE is returned. The strstr() function also has a function of the same name, strchr().
5. String and ASCII code
4. Comparison of strings
The comparison function is
strcmp() // case sensitive
strcasecmp()// is case insensitive
strncmp() // comparison section
strncasecmp()// case insensitive, compare parts
5. String substitution
str_replace(search,replace,subject)
Replace the search string in the string subject with the new string replace
< ?php
$str="I love you";
$replace="lucy";
$end=str_replace("you",$replace,$str);
echo $end; // output "I love lucy"
? >
Case-sensitive, you can also do many-to-1. Many-to-many substitution, but you can't do 1-to-many substitution.
< ?php
$str="What Is Your Name";
$array=array("a","o","A","O","e");
echo str_replace ($array, "", $str); // many-to-one substitution, output "Wht Is Yur Nm"
$array1=array("a","b","c");
$array2=array("d","e","f");
echo str_replace ($array1, $array2, "abcdef"); // many-to-many substitution, output "defdef"
? >
substr_replace
Replace part 1 of the string.
6. String with HTML
slightly
7. Other string functions
1. Strings and arrays
a. String is converted to an array
The explode() function splits another string with the specified string and returns an array
< ?php
$str=" split string with space ";
array=explode(" ", $str);
pint_r($array);
Output Array ([0] = > The use of [1] = > Space [2] = > [3] = segmentation > String)
? >
The b. Array is converted to a string
implode(string $glue,array $pieces)
$pieces is an array of strings to concatenate, and $glue is the concatenator used to concatenate strings. Such as:
< ?php
$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 this function 1 cannot be converted to its original form once it is encrypted.
4.3 instance message book content processing
1 guest book, which contains the Email address and user's message, extracts the customer's Email address and message, and requires the Email address not to have a ". "or a comma", "before the @ sign.
Take the content before the @ symbol in the Email address as the user's user name, and change the first person "I" in the user's comments to "I".
 
<form name="f1" method="post" action=""> 
<font face=" Founder ShuTi " size=4> your Email Address: </font><br> 
<input type="text" name="Email" size=31><br> 
<font face=" Founder ShuTi " size=4> Your message: </font><br> 
<textarea name="note" rows=10 cols=30></textarea> 
<br><input type="submit" name="bt1" value=" submit "> 
<input type="reset" name="bt2" value=" empty "> 
</form> 
<!-- That's the guest book form --> 
<?php 
if(isset($_POST['bt1'])) 
{ 
$Email=$_POST['Email']; // receive Eamil address  
$note=$_POST['note']; // Receive a message  
if(!$Email||!$note) // Determines whether to get a value  
echo "<script>alert('Email Please complete the address and message! ')</script>"; 
else 
{ 
$array=explode("@", $Email); // segmentation Email address  
if(count($array)!=2) // If I have two @ The sign says wrong  
echo "<script>alert('Email Address format error! ')</script>"; 
else 
{ 
$username=$array[0]; // achieve @ The content before the symbol  
$netname=$array[1]; // achieve @ The content after the symbol  
// if username Contains" . "Or" , "An error  
if(strstr($username,".") or strstr($username,",")) 
echo "<script>alert('Email Address format error! ')</script>"; 
else 
{ 
$str1= htmlspecialchars("<"); // Output symbol" < "  
$str2= htmlspecialchars(">"); // Output symbol" > "  
// Replace "I" in your message with "I"  
$newnote=str_replace(" I "," I am ",$note); 
echo "<font face=' blackbody ' size=4>"; 
echo " The user ". $str1. $username . $str2. " How do you do ! "; 
echo " You are a ". $netname. " The net friend !<br>"; 
echo "<br> Your message is: <br>    ".$newnote."<br>"; 
echo "</font>"; 
} 
} 
} 
} 
?> 

Related articles: