One, string substitution
Str_replace ("Iwind", "Kiki", "I love Iwind, Iwind said""I love Kiki, Kiki said"
Str_replace (find,replace,string,count) parameter description
Find required. Specifies the value to find.
Replace Required. Specifies the value that replaces the value in Find.
string is required. Specifies the string to be searched.
Count is optional. A variable that counts the number of replacements.
Second, string deletion
Method One
$string = ' fdjborsnabcdtghrjosthabcrgrjtabc '; $string Preg_replace ('/[abc]+/i ', ',$string);
Method Two
Convert a string to an array
$arr = str_split ( $string foreach ( $arr as $key = = $value if ( ( $value , array (' A ', ' B ' , ' C ' unset ( $arr [ $key $string = implode (", $arr );
Third, string interception
<?//construct a string $str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Echo"Original string:".$str.""; //interception by various means $str 1=substr($str, 5); Echo"Start from the 5th character to the last:".$str 1.""; $str 2=substr($str, 9,4); Echo"Start with the 9th character and take 4 characters:".$str 2.""; $STR 3=substr($str,-5); Echo"Take the bottom 5 characters:".$STR 3.""; $STR 4=substr($str, -8,4); Echo"Start from the last 8th character and take 4 characters backwards:".$STR 4.""; $STR 5=substr($str, -8,-2); Echo"From the last 8th character to the bottom of the 2nd character:".$STR 5.""; ?>
Support mixed interception in Chinese and English.
/*------------------------------------------------------parameter: $str _cut the string to be truncated $length Maximum length allowed for string display program features: Capture full-width and half-width (kanji and English) mixed strings to avoid garbled------------------------------------------------------*/functionSubstr_cut ($str _cut,$length){ if(strlen($str _cut) >$length) { for($i= 0;$i<$length;$i++) if(Ord($str _cut[$i]) > 128)$i++; $str _cut=substr($str _cut, 0,$i).".."; } return $str _cut;}?>
Four, string comparison
In PHP, you can compare strings with = = (double equals) or = = = (three equals). The difference between the two is that the double equals is not a comparison type, the three equals is the comparison type, it does not convert the type, and if there is a number type value on both sides of the equal sign, the second value is converted to a number and then compared. In this case, if it is a pure string or null, it is converted to 0 for comparison. Similarly, the size of the number is the same as the equal sign, which may result in incorrect results when compared.
Therefore, the comparison string can be used with PHP's own function strcmp and strcasecmp. Where strcasecmp is a variant of strcmp, it converts the string to lowercase before comparing it. The following code:
Var_dump (0 = = ' Test'var_dump(0 = ='var_dump(5 > ' T'var_dump( strcmp(5, ' T '));
The result is (the 1th to 3rd result is wrong, only the 4th one is right):
BOOL (true) bool (TRUE)bool(true) int (
Five, String lookup
The Strstr () function is used to get a substring that specifies the first occurrence of a string in another string to the end of the latter and, if successful, returns the remaining string (there is a matching character), or false if no matching character is found.
syntax:string strstr (String haystack,string needle)
parameter:haystack: The necessary parameter that specifies the string from which to search.
parameter:needle: Required parameter, specifies the object to search, and if the parameter is a numeric value, the character that matches the ASCII value of this numeric value is searched.
The instance code is as follows:
<? echostrstr("Tomorrow's Programming Dictionary", "compilation"); // echo "<br>"; // echostrstr("www.phpfensi.com", "111"); // echo "<br>"; // echostrstr("0431-84972266″," 8″); //
definition and Usage:the Strpos () function returns the position of the first occurrence of a string in another string, or False if the string is not found.
Syntax:strpos (String,find,start)
parameters:string required, which specifies the strings to be searched.
parameters:Find required, specifies the character to find.
parameters:Start is optional and specifies where to start the search.
Note: This function is case-sensitive and uses the Stripos () function if you want to search for case insensitive.
The instance code is as follows:
<? echostrpos("Hello world!", "Wo"?>//
Six, string case conversion
<?PHP$foo= ' Hello world! ';$foo=Ucwords($foo);//Hello world! $bar = ' Hello world! ';$bar=Ucwords($bar);//HELLO world!$bar=Ucwords(Strtolower($bar));//Hello world!?>capitalize the first letter of the initial word:Ucfirst()<?PHP$foo= ' Hello world! ';$foo=Ucfirst($foo);//Hello world! $bar = ' Hello world! ';$bar=Ucfirst($bar);//HELLO world!$bar=Ucfirst(Strtolower($bar));//Hello world!?>first word initials lowercase lcfirst ()<?PHP$foo= ' HelloWorld ';$foo= Lcfirst ($foo);//HelloWorld$bar= ' HELLO world! ';$bar= Lcfirst ($bar);//HELLO world!$bar= Lcfirst (Strtoupper($bar));//HELLO world!?>Uppercase letters:Strtoupper() The letter becomes lowercase:Strtolower()
Seven, String cutting
<? PHP $str= "1|2|3|4|5|" ; $var=explode("|",$str); Print_r ($var);? >$var=explode("|",$str);
Divide the $str by |, and PHP has other characters that divide the string by the specified character into the array.
Str_split (string,length) parameter description
string is required. Specifies the string to be split.
Length is optional. Specifies the length of each array element. The default is 1.
Json_decode () This function can also divide a string into an array (the second argument is true).
Bill: PHP string manipulation (string substitution, delete, intercept, copy, join, compare, find, include, case transform, cut array, etc.)