PHP string operation (string replacement, deletion, truncation, copy, connection, comparison, search, including, case conversion, cutting to array, etc.)

Posted by lazy_yogi on Sat, 11 Apr 2020 11:46:08 +0200

1, String substitution

str_replace("iwind", "kiki", "i love iwind, iwind said");
//Will output "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 to replace the value in find.  
String required. Specifies the string to be searched.  
count is optional. A variable that counts the number of replacements.

2, String delete

Method 1

$string = 'fdjborsnabcdtghrjosthabcrgrjtabc';
$string = preg_replace('/[abc]+/i','',$string);

Method two

Convert string to array

$arr = str_split($string);
foreach( $arr as $key => $value ){
if( in_array($value,array('a','b','c')) ){
$string = implode('',$arr);

3, String truncation

  //Construction string
  echo "Original string:".$str."
  //Intercept in various ways
  $str1 = substr($str,5);
  echo "From the 5th character to the last:".$str1."
  $str2 = substr($str,9,4);
  echo "Take 4 characters from the 9th character:".$str2."
  $str3 = substr($str,-5);
  echo "Take the last 5 characters:".$str3."
  $str4 = substr($str,-8,4);
  echo "Take 4 characters back from the 8th to last character:".$str4."
  $str5 = substr($str,-8,-2);
  echo "From the 8th to the 2nd character:".$str5."

Support Chinese and English mixed interception.

$str_cut    String to be truncated
$length     Maximum length of string display allowed
 Program function: intercept the mixed character string of full angle and half angle (Chinese and English) to avoid garbled code
function substr_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;

4, String comparison

In PHP, you can use = = (double equal sign) or = = = (triple equal sign) to compare strings. The difference between the two is that the double equal sign does not compare the type, and the third equal sign will compare the type, which does not convert the type; when comparing with the double equal sign, if there is a value of number type on the left and right sides of the equal sign, just convert another value to a number, and then compare. In this case, if it is a pure string or NULL, it will be converted to 0 for comparison. Similarly, the size on sign is the same as the equal sign, and incorrect results may appear during comparison.
Therefore, you can use PHP's own functions strcmp and strcasecmp to compare strings. Among them, strcasecmp is a variant of strcmp, which first converts strings to lowercase and then compares them. The following code:

var_dump(0 == 'Test'); 
var_dump(0 == ''); 
var_dump(5 > 'T'); 
var_dump(strcmp(5, 'T'));

The results are (the first three results are wrong, only the fourth one is right):


5, String lookup

The strstr() function is used to get a substring from the first occurrence of a specified string in another string to the end of the latter. If the execution is successful, it returns the remaining string (with matching characters); if no matching characters are found, it returns false.

Syntax: string strstr (string haystack, string need)

Parameter: haystack: required parameter to specify which string to search from.

Parameter: needle: required parameter, which specifies the object to search. If the parameter is a numeric value, characters matching the ASCII value of the numeric value will be searched.

The example code is as follows:

echo strstr("Tomorrow programming dictionary ","Edit ");  //Output the string of the query 
echo "<br>";                      //Carriage return 
echo strstr("","111");  //Output the string of the query (from the first m) 
echo "<br>";                         //Carriage return 
echo strstr("0431-84972266″,"8″);    //Output the string of the query 

Definition and usage: the strpos() function returns the first occurrence of a string in another string, or false if the string is not found.

Syntax: strpos(string,find,start)

Parameter: string required, specifies the string to be searched.

Parameter: find required, specifies the character to find.

Parameter: start optional, specify the location to start the search.

Note: this function is case sensitive. For case insensitive searches, use the stripos() function.

The example code is as follows:

echo strpos("Hello world!","wo"); 
?>//Output: 6 

6, String case conversion

$foo = 'hello world!';
$foo = ucwords($foo); // Hello World!$bar = 'HELLO WORLD!';
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
//First initial capitalized: ucfirst()
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
//First initial small lcfirst()
$foo = 'HelloWorld';
$foo = lcfirst($foo); // helloWorld
$bar = 'HELLO WORLD!';
$bar = lcfirst($bar); // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
//Capitalize: strtoupper()
//Letter to lowercase: strtolower()

7, String cutting


Divide $str by |, php has other ways to divide string specified characters into arrays
str_split(string,length) parameter description
String required. Specifies the string to split.
Length is optional. Specifies the length of each array element. The default is 1.
The function json_decode() can also split strings into arrays (the second parameter is true)

Article from:

Topics: PHP ascii Programming