Home > Backend Development > PHP Tutorial > Summarize the usage of PHP string and array processing functions

Summarize the usage of PHP string and array processing functions

coldplay.xixi
Release: 2023-04-09 13:30:01
forward
2351 people have browsed it

Summarize the usage of PHP string and array processing functions

本文实例讲述了PHP字符串与数组处理函数用法。分享给大家供大家参考,具体如下:

字符串处理函数

trim --去除字符串首尾的多余空白字符和其他字符

函数结构:

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

第一个参数是咱要处理的字符串,第二个参数是要排除的字符(默认 \t\n\r\0\x0B)

相关学习推荐:php编程(视频)

str_replace --更换子串

函数结构:

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

解释起来太麻烦,我们来看实例:

实例1

$str1 = str_replace('%name%', 'LargerK', 'my name is %name%');
echo $str1;   // 输出 my name is LargerK
Copy after login

实例2

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

实例3

$str1 = str_replace(["KFC", "可乐", "薯条"], ["披萨", "酥皮汤", "西冷牛排"], '我想吃KFC 点个薯条和可乐');
echo $str1;   // 我想吃披萨 点个西冷牛排和酥皮汤
Copy after login

实例4

$count = 0;
$str1 = str_replace("oo", "~~", "ooop good... so cool", $count);
echo $str1 . "<br />";   // 输出~~op g~~d... so c~~l
echo $count;        // 输出 3
Copy after login

strlen --返回字符串的长度

int strlen ( string $string )
Copy after login

实例:

echo strlen(&#39;hello k&#39;);   // 7
Copy after login

数组处理函数

array_diff --对比数组,取出差集

array array_diff ( array $array1 , array $array2 [, array $... ] )
Copy after login

说明:拿到第一个数组,跟第二个第三个等做比较,然后返回一个数组。

返回的数组的内容:只存在于第一个数组中,第二个和更多的比对数组中都没有的元素。

实例1

$array1 = [&#39;1&#39;, &#39;name&#39; => &#39;alex k&#39;, &#39;age&#39; => 24, &#39;desire&#39; => &#39;Web developer&#39;];
$array2 = [&#39;title&#39; => &#39;alex k&#39;, &#39;age&#39; => 23, &#39;desire&#39; => &#39;Web developer&#39;];
// 需要注意的是,它只匹配value而忽略key
print_r(array_diff($array1, $array2)); // Array ( [0] => 1 [age] => 24 )
Copy after login

array_slice --从数组中取出一段

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
Copy after login
  • 第一个参数:源数组。
  • 第二个参数:从哪里开始取,如果是负数 则从最后一个元素开始算。
  • 第三个参数:取多少 不指定的话默认取所有元素。
  • 第四个参数:默认会把数组的数字索引重置,设置为true则不会改变。

实例1

$array = [&#39;php&#39;, &#39;html&#39;, &#39;css&#39;, &#39;sql&#39;, &#39;laravel&#39;];
$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 )
Copy after login

实例2

$array = [&#39;php&#39;, &#39;html&#39;, &#39;css&#39;, &#39;sql&#39;, &#39;laravel&#39;];
$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 )
Copy after login

实例3

$array = [&#39;php&#39;, &#39;html&#39;, &#39;css&#39;, &#39;sql&#39;, &#39;laravel&#39;];
$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 )
Copy after login

实例4

$array = [&#39;php&#39;, &#39;html&#39;, &#39;css&#39;, &#39;sql&#39;, &#39;laravel&#39;];
$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 )
Copy after login

array_unique --删除数组中重复的值

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Copy after login
  • 第一个参数:需要过滤的数组。
  • 第二个参数:排序方式,1.SORT_REGULAR - 按照通常方法比较(不修改类型) 2.SORT_NUMERIC - 按照数字形式比较 3.SORT_STRING - 按照字符串形式比较 4.SORT_LOCALE_STRING - 根据当前的本地化设置,按照字符串比较。

实例

$array = [&#39;a&#39; => &#39;blue&#39;, &#39;yellow&#39;, &#39;b&#39; => &#39;black&#39;, &#39;blue&#39;, &#39;c&#39; => &#39;black&#39;];
$result = array_unique($array);
print_r($result);    // Array ( [a] => blue [0] => yellow [b] => black )
Copy after login

相关学习推荐:编程视频

The above is the detailed content of Summarize the usage of PHP string and array processing functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
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