Summary of PHP string and array handler usage

Time:2021-10-23

This article describes the use of PHP string and array processing functions. Share with you for your reference, as follows:

String handler

Trim — remove extra white space and other characters at the beginning and end of the string

Function structure:


string trim ( string $str [, string $character_mask = " \t\n\r

string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )
\x0B" ] )

The first parameter is the string to be processed, and the second parameter is the character to be excluded (default \ t \ n \ R \ 0 \ x0B)

str_ Replace — replace substring

Function structure:


mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

It’s too cumbersome to explain. Let’s take an example:

Example 1

$str1 = str_replace('%name%', 'LargerK', 'my name is %name%');
echo $str1;   //  Output my name is largerk

Example 2

$str1 = str_replace(['s', 'a', 't'], '111', 'this is an apple');
echo $str1;   //  Output 111hi111 i111 111n 111pple

Example 3

$str1 = str_ Replace (["KFC", "coke", "French fries"], ["pizza", "Pastry Soup", "Sirloin Steak"], 'I want KFC to order French fries and Coke');
echo $str1;   //  I'd like pizza, sirloin steak and Pastry Soup

Example 4

$count = 0;
$str1 = str_replace("oo", "~~", "ooop good... so cool", $count);
echo $str1 . "<br />";   //  Output ~ ~ OP G ~ ~ d... So C ~ ~ l
echo $count;        //  Output 3

Strlen — returns the length of the string


int strlen ( string $string )

example:


echo strlen('hello k');   // 7

Array handler

array_ Diff — compare the array and get the difference set


array array_diff ( array $array1 , array $array2 [, array $... ] )

Description: get the first array, compare it with the second and third, and then return an array.

The contents of the returned array: only exist in the first array, the second and more elements that are not in the comparison array.

Example 1

$array1 = ['1', 'name' => 'alex k', 'age' => 24, 'desire' => 'Web developer'];
$array2 = ['title' => 'alex k', 'age' => 23, 'desire' => 'Web developer'];
//Note that it only matches value and ignores key
print_r(array_diff($array1, $array2)); // Array ( [0] => 1 [age] => 24 )

array_ Slice — take a segment from the array


array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
  • First parameter: source array.
  • The second parameter: where to start. If it is a negative number, it starts from the last element.
  • The third parameter: if the number is not specified, all elements will be taken by default.
  • The fourth parameter: the numeric index of the array will be reset by default. If it is set to true, it will not change.

Example 1


$array = ['php', 'html', 'css', 'sql', 'laravel'];
$slice1 = array_slice($array, 1);
$slice2 = array_slice($array, -2);
print_r($slice1);  // Array ( [0] => html [1] => css [2] => sql [3] => laravel )
print_r($slice2);  // Array ( [0] => sql [1] => laravel )

Example 2


$array = ['php', 'html', 'css', 'sql', 'laravel'];
$slice1 = array_slice($array, 1, 2);
$slice2 = array_slice($array, -2, 1);
print_r($slice1);  // Array ( [0] => html [1] => css )
print_r($slice2);  // Array ( [0] => sql )

Example 3


$array = ['php', 'html', 'css', 'sql', 'laravel'];
$slice1 = array_slice($array, 1, -1);
$slice2 = array_slice($array, -3, -1);
print_r($slice1);  // Array ( [0] => html [1] => css [2] => sql )
print_r($slice2);  // Array ( [0] => css [1] => sql )

Example 4


$array = ['php', 'html', 'css', 'sql', 'laravel'];
$slice1 = array_slice($array, 1, -1);
$slice2 = array_slice($array, 1, -1, true);
print_r($slice1);  // Array ( [0] => html [1] => css [2] => sql )
print_r($slice2);  // Array ( [1] => html [2] => css [3] => sql )

array_ Unique — remove duplicate values from the array


array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
  • The first parameter: the array to be filtered.
  • Second parameter: sorting method, 1. Sort_ Regular – compare according to the usual method (do not modify the type)   2.SORT_ Numeric – compare in numeric form   3.SORT_ String – compare as a string   4.SORT_ LOCALE_ String – compare by string based on the current localization settings.

example


$array = ['a' => 'blue', 'yellow', 'b' => 'black', 'blue', 'c' => 'black'];
$result = array_unique($array);
print_r($result);    // Array ( [a] => blue [0] => yellow [b] => black )

For more information about PHP, readers who are interested can see the special topics on this site: summary of common PHP functions and skills, summary of PHP string usage, complete collection of PHP array operation skills, introduction to PHP basic syntax, introduction to PHP + MySQL database operation, and summary of common PHP database operation skills

I hope this article will help you in PHP programming.