PHP字符串与数组惯用函数
PHP字符串与数组常用函数
字符串:
字符串的连接:PHP中用英文的点号.来连接两个字符串。
去除字符串首尾的空格:trim去除一个字符串两端空格。
rtrim是去除一个字符串右部空格,其中的r是right的缩写。
ltrim是去除一个字符串左部空格,其中的l是left的缩写。
获取字符串的长度:php中有一个神奇的函数,可以直接获取字符串的长度,这个函数就是strlen()。但是如果有中文汉字,可以使用mb_strlen()函数获取字符串中中文长度。
字符串的截取:英文字符串的截取函数substr(),函数说明:substr(字符串变量,开始截取的位置,截取个数)
中文字符串的截取函数mb_substr(),函数说明:mb_substr(字符串变量,开始截取的位置,截取个数, 网页编码)
字符串的查找:查找字符串,我们需要用到PHP的查找字符串函数strpos();函数说明:strpos(要处理的字符串, 要定位的字符串, 定位的起始位置[可选])
字符串的替换:替换字符串,我们需要用到PHP的替换函数str_replace();函数说明:str_replace(要查找的字符串, 要替换的字符串, 被搜索的字符串, 替换进行计数[可选])
字符串格式化:PHP的格式化字符串函数sprintf();函数说明:sprintf(格式, 要转化的字符串);返回:格式化好的字符串
例子:
$str = '99.9';$result = sprintf('%01.2f', $str);echo $result;//结果显示99.90
这个 %01.2f 是什么意思呢?
1、这个 % 符号是开始的意思,写在最前面表示指定格式开始了。 也就是 "起始字符", 直到出现 "转换字符" 为止,就算格式终止。
2、跟在 % 符号后面的是 0, 是 "填空字元" ,表示如果位置空着就用0来填满。
3、在 0 后面的是1,这个 1 是规定整个所有的字符串占位要有1位以上(小数点也算一个占位)。
如果把 1 改成 6,则 $result的值将为 099.90
因为,在小数点后面必须是两位,99.90一共5个占位,现在需要6个占位,所以用0来填满。
4、在 %01 后面的 .2 (点2) 就很好理解了,它的意思是,小数点后的数字必须占2位。 如果这时候,$str 的值为9.234,则 $result的值将为9.23.
为什么4 不见了呢? 因为在小数点后面,按照上面的规定,必须且仅能占2位。 可是 $str 的值中,小数点后面占了3位,所以,尾数4被去掉了,只剩下 23。
5、最后,以 f "转换字符" 结尾。
字符串的合并:php字符串合并函数implode();函数说明:implode(分隔符[可选], 数组);返回值:把数组元素组合为一个字符串
例子:
$arr = array('Hello', 'World!');$result = implode('', $arr);print_r($result);//结果显示Hello World!
字符串的分割:php字符串分隔函数explode();函数说明:explode(分隔符[可选], 字符串);返回值:函数返回由字符串组成的数组
例子:
$str = 'apple,banana';$result = explode(',', $str);print_r($result);//结果显示array('apple','banana')
字符串的转义:php字符串转义函数addslashes();函数说明:用于对特殊字符加上转义字符,返回一个字符串;返回值:一个经过转义后的字符串
例子:
$str = "what's your name?";echo addslashes($str);//输出:what\'s your name?
数组:
foreach循环访问索引数组里的值:foreach循环可以将数组里的所有值都访问到,下面我们展示下,用foreach循环访问索引数组里的值。
例如:
$fruit=array('苹果','香蕉','菠萝'); foreach($fruit as $k=>$v){ echo '<br>第'.$k.'值是:'.$v; }
关联数组初始化:PHP有两种数组:索引数组、关联数组。
可以使用下面代码实现:
$fruit = array( 'apple'=>"苹果", 'banana'=>"香蕉", 'pineapple'=>"菠萝" );
可以使用print_r($fruit);语句输出数组键及对应的值。
关联数组赋值:关联数组赋值有两种方式:
第一种:用数组变量的名字后面跟一个中括号的方式赋值,当然,关联数组中,中括号内的键一定是字符串。比如,$arr['apple']='苹果';
第二种:用array()创建一个空数组,使用=>符号来分隔键和值,左侧表示键,右侧表示值。当然,关联数组中,键一定是字符串。比如,array('apple'=>'苹果');
访问关联数组内容:用数组变量的名字后跟中括号+键的方式来访问数组中的值,键使用单引号或者双引号括起来。
比如:
$fruit = array('apple'=>"苹果",'banana'=>"香蕉",'pineapple'=>"菠萝"); $fruit0 = $fruit['banana']; print_r($fruit0);
foreach循环访问关联数组里的值:foreach循环可以将数组里的所有值都访问到,下面我们展示下,用foreach循环访问关联数组里的值。
例如:
$fruit=array('apple'=>"苹果",'banana'=>"香蕉",'pineapple'=>"菠萝"); foreach($fruit as $k=>$v){ echo '<br>水果的英文键名:'.$k.',对应的值是:'.$v; }

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

AI Hentai Generator
Generate AI Hentai for free.

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



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

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

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

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

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"

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

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

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
