Home php教程 PHP源码 php array数组的相关处理函数and str字符串处理与正则表达式

php array数组的相关处理函数and str字符串处理与正则表达式

Jun 08, 2016 pm 05:22 PM
array nbsp quot str

下面给各位同学整理了一些关于php array数组的相关处理函数and str字符串处理与正则表达式,希望文章对你会有所帮助。

<script>ec(2);</script>

数组的相关处理函数:

1)数组的键值操作函数
 
array_values();//获取数组中的值
array_keys();//获取数组中的键
in_array();//检查一个值是否在数组中
array_key_exists();//检查一个键是否在数组中
array_flip();//键和值对调
array_reverse();//数组中的值反转

2)统计数组的元素和唯一性

count();//统计数组的个数
array_count_values();//统计数组中值出现的次数
array_unique();//删除数组中重复值

3)使用回调函数处理数组的函数
 
array_filter();//数组值过滤
array_map();//将回调函数作用到给定数组的单元上

4)数组的排序函数

 
sort();
//按值把数组值进行排序,升序,不保留key
rsort();
//按值把数组值进行排序,降序,不保留key
 
asort();
//按值把数组值进行排序,升序,保留key
arsort();
//按值把数组值进行排序,降序,保留key
 
ksort();
//按键把数组值进行排序,升序,保留key
krsort();
//按键把数组值进行排序,降序,保留key
 
natsort();
//按自然数排序
natcasesort();
//忽略大小写的自然数排序
 
array_multisort();//用一个数组对另外一个数组进行排序
SORT_DESC倒序
SORT_ASC正序  array_multisort($arr2,SORT_DESC,$arr);

5)拆分、合并、分解与结合函数

 
array_slice();//截取一段值 $arr2=array_slice($arr,0,3); 0开始位置,3个 ,返回值:截取的内容
 
array_splice();//截取一段值,保留一段值  
 
返回值:截取后剩下的内容  //array_splice(3,3,"aa"); 从下标3的位置开始向后截取3个值,aa替换截取的内容
 
array_combine();//合并,一个是key,一个是value eg: $arr3=array_combine($arr1,$arr2); $arr1为key值 ,$arr2为值
 
array_merge();  并集  //合并,键值相同,后面覆盖前面 返回一个新数组 $arr3=array_merge($arr1,$arr2);
 
array_intersect();//交集
 
array_diff();//差集
 
implode();//把数组连接成字符串  eg: $str=implode("|" ,$arr);  | 分割符
  
explode();//把字符串分解成数组   eg: $arr=explode('|',$str);


6)数组与数据结构

 
array_pop();//从最后弹出一个值,返回弹出值 //unset($arr[count($arr)-1]);
array_push(); array_push($arr,6);//从最后添加一个值,返回数组个数
//$arr[]="aa";
array_shift();//从前面弹出一个值,返回移出值,原数组下标重排
//unset($arr[0]); 原数组下标不重排
array_unshift();//从前面插入一个值,返回数组个数

7)其他有用的数组处理函数

 
array_rand();//随机取一个key
 
shuffle();//打乱数组
 
array_sum();//数组所有值的和
 
range();//获取一个范围内数组
//range(1,10); 返回数组 array(1,2,3,...,10);
//range(1,10,2); array(1,3,5,7,9);  2代表差值;默认是0不写

字符串处理与正则表达式
————————————————

1.字符串的处理介绍
2.常用的字符串输出函数
3.常用的字符串格式化函数
4.字符串比较函数
5.正则表达式在字符串中的应用
6.与perl兼容的正则表达式函数

字符串的输出:

 
1)echo "hello world www.111cn.net";
2)print "aaaa";
3)die("输出一条错误消息");
4)printf("--%s----%s--",$a,$b);
%s  字符串
%d  数字
%f  浮点型   //%.2f   小数点后面两位
5)sprintf("$s%s",$a,$b);
不直接输出,而是返回值给一个新的变量

常用的字符串格式化函数:
1.去除空格和字符串填补函数
 
ltrim() //去左边空格
rtrim() //去右边空格;
trim()  //去掉两头空格  $str='abc'; trim($str,'b'); echo $str ; 结果 :ac; 可以删除指定的字符串
str_pad() //向字符串里添加空格或字符串
 
$input = "Alien";
echo str_pad($input, 10);  // 输出 "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // 输出 "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // 输出 "__Alien___"
echo str_pad($input, 6 , "___");   // 输出 "Alien_"
?>

2.字符串大小写转换函数

 
strtolower()
strtoupper()
ucfirst()
ucwords()

3.与html标签相关联的字符串函数

 
nl2br()
htmlspecialchars()
strip_tags()
addslashes()
stripslashes()

4.其他字符串格式化函数

 
strrev()
strlen()//返回字符串的长度
number_format()
md5()   //单向不可逆加密
str_shuffle()  //随机输出字符串

字符串比较函数:
1.按字节进行字符串的比较
1
 
strcmp($str1,$str2) //比较字符串的每个字节
 
strcasecmp()//忽略大小写比较字符串的每个字节

2.按自然排序法时字符串的比较

 
strnatcmp();
//按自然排序比较字符串中的数字

字符串的分割与拼接:
1.分割
 
//把字符串分割成数组
explode()
preg_split('//',$str);

2.拼接
//把数组拼接成字符串

 
implode()
join()  //等于implode();

字符串的截取:

 
substr()

字符串的查找:
 
strstr()//查找指定字符在字符串中的第一次出现
 
 
strrchr()//查找指定字符在字符串中的最后一次出现
 
 
strpos()//w在$str中第一次出现的位置
strrpos($str,'w')//w在$str中最后一次出现的位置

字符串的替换:
1
 
str_replace()

支持多字节文字

 
mb_substr($str,0,7,"utf-8");
mb_strpos();
mb_strrpos();
mb_strstr();
mb_strtoupper();
mb_strtolower();

正则表达式在字符串中的应用:
一.正则表达式介绍
正则表达式是用于描述字符排列和匹配模式的一种语法规则,它主要用于字符串的模式分割,匹配,查找及替换操作,在php在正则表

达式一般是由正规字符和一些特殊字符联合构成的一个文本模式的程序性描述,这在儿我们使用perl兼容正则表达式
二.正则表达式语法
1.原子 www.111cn.net
1)单个字符,数字
a-z,A-Z,0-9   a-z其中任意一个字符
2)模式单元
(abc) 匹配abc并且成一个单元
3)原子表
[abc]它中的任意一个字符a或b或c
4)重新使用的模式单元
\1,$1
5)普通转义字符
d,D,w,W,s,S
d 匹配一个数字
D 匹配一个非数字
w 匹配字母,数字,下划线
W 除了字母,数字,下划线
s 匹配空白字符,空格,tab
S 除了空白字符,空格,tab
6)转义元字符
*,. []
2.元字符
*,+,?,|,^,$,b,B,[],[^],{m},{m,n},{m,},(),.
d* 一个或多个或0个数字
d+ 一个或多个数字
d? 一个或0个数字
.  任意一个字符
3.模式修正符
i,m,s,U,e
i  忽略大小写
m  视为多行
s  视为一行
U  贪婪模式,最大模式
e  替换的时候用的,可以用函数加工向后引用\1,$1
三.字符串正则表达式函数
1.字符串的匹配与查找
preg_match()
preg_match_all()
preg_grep()
2.字符串的替换
preg_replace()
//问题:正则e修饰符
3.字符串的分割与连接
preg_split()
4.正则表达式的web验证应用
1)电子邮件地址
2)url地址
3)电话号码
 
 
ubb编辑器:
 
[url][/url][b]文字[/b]

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles