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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools