


Summarize the usage of PHP string and array processing functions
本文实例讲述了PHP字符串与数组处理函数用法。分享给大家供大家参考,具体如下:
字符串处理函数
trim --去除字符串首尾的多余空白字符和其他字符
函数结构:
string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )
第一个参数是咱要处理的字符串,第二个参数是要排除的字符(默认 \t\n\r\0\x0B)
相关学习推荐:php编程(视频)
str_replace --更换子串
函数结构:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
解释起来太麻烦,我们来看实例:
实例1
$str1 = str_replace('%name%', 'LargerK', 'my name is %name%'); echo $str1; // 输出 my name is LargerK
实例2
$str1 = str_replace(['s', 'a', 't'], '111', 'this is an apple'); echo $str1; // 输出 111hi111 i111 111n 111pple
实例3
$str1 = str_replace(["KFC", "可乐", "薯条"], ["披萨", "酥皮汤", "西冷牛排"], '我想吃KFC 点个薯条和可乐'); echo $str1; // 我想吃披萨 点个西冷牛排和酥皮汤
实例4
$count = 0; $str1 = str_replace("oo", "~~", "ooop good... so cool", $count); echo $str1 . "<br />"; // 输出~~op g~~d... so c~~l echo $count; // 输出 3
strlen --返回字符串的长度
int strlen ( string $string )
实例:
echo strlen('hello k'); // 7
数组处理函数
array_diff --对比数组,取出差集
array array_diff ( array $array1 , array $array2 [, array $... ] )
说明:拿到第一个数组,跟第二个第三个等做比较,然后返回一个数组。
返回的数组的内容:只存在于第一个数组中,第二个和更多的比对数组中都没有的元素。
实例1
$array1 = ['1', 'name' => 'alex k', 'age' => 24, 'desire' => 'Web developer']; $array2 = ['title' => 'alex k', 'age' => 23, 'desire' => 'Web developer']; // 需要注意的是,它只匹配value而忽略key print_r(array_diff($array1, $array2)); // Array ( [0] => 1 [age] => 24 )
array_slice --从数组中取出一段
array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )
- 第一个参数:源数组。
- 第二个参数:从哪里开始取,如果是负数 则从最后一个元素开始算。
- 第三个参数:取多少 不指定的话默认取所有元素。
- 第四个参数:默认会把数组的数字索引重置,设置为true则不会改变。
实例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 )
实例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 )
实例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 )
实例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 --删除数组中重复的值
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
- 第一个参数:需要过滤的数组。
- 第二个参数:排序方式,1.SORT_REGULAR - 按照通常方法比较(不修改类型) 2.SORT_NUMERIC - 按照数字形式比较 3.SORT_STRING - 按照字符串形式比较 4.SORT_LOCALE_STRING - 根据当前的本地化设置,按照字符串比较。
实例
$array = ['a' => 'blue', 'yellow', 'b' => 'black', 'blue', 'c' => 'black']; $result = array_unique($array); print_r($result); // Array ( [a] => blue [0] => yellow [b] => black )
相关学习推荐:编程视频
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
