php字符串操作函数入门篇
php教程字符串操作函数入门篇
1.字符串的定义与显示
定义:通过””,''来标志
显示:echo()和print(),但print()具有返回值值,1,而echo()没有,但echo比print()要快,print()能用在复合语句中。
2.字符串的格式化
printf(string $format[,mixed$args])
第一参数是格式字符串,$args是要替换进来的值,prinf(“%d”,$num);
说明,如果想打印一个”%”,必须用”%”,浮点数f,八进制用”0”
3.常用的字符串函数
1.计算字符串的长度
strlen(string $string),说明,1个英文长度1个字符,1个汉字长度为2个字符,空格也算一个字符。
2.将字符串改变大小写
转为小写:strtolower()
转为大写:strtoupper()
将第一个字符大写: ucfirst()
将每个单词的第一个字母大写 ucwords()
3.字符串裁剪。
当一个字符串的首尾有多余的空白字符,如空格、制表符等可以用
string trim(string $str[,string $charlist])
string rtrim(string $str[sring $charlist])
string itrim(string $str[,string $charlist])
表4.1 trim、itrim、rtrim函数的默认删除字符
字 符
ASCII码
意 义
" "
32(0x20)
空格
"t"
9(0x09)
制表符
"n"
10(0x)
换行
"r"
13(0x0D)
回车
""
0(0x00)
空字节
"x0B"
11(0x0B)
垂直制表符
4.字符串的查找
string strstr(string $a, string $b)
说明:strstr()函数用于查找字符串指针$b在字符串$a中出现的位置,
并返回$a字符串中从$b开始到$a字符串结束处的字符串。
如果没有返回值,即没有发现$b,则返回FALSE。strstr()函数还有一个同名函数strchr()。
5.字符串与ASCII码
4.字符串的比较
比较函数有
strcmp() //区分大小写
strcasecmp()//不区分大小写
strncmp() //比较部分
strncasecmp()//不区分大小写,比较部分
5.字符串的替换
str_replace(search,replace,subject)
说明使用新的字符串replace替换字符串subject中的search字符串
$str="I love you";
$replace="lucy";
$end=str_replace("you",$replace,$str);
echo $end; //输出"I love lucy"
?>
对大小写敏感,还可实现多对一、多对多的替换,但无法实现一对多的替换。
$str="What Is Your Name";
$array=array("a","o","A","O","e");
echo str_replace($array, "",$str); //多对一的替换,输出"Wht Is Yur Nm"
$array1=array("a","b","c");
$array2=array("d","e","f");
echo str_replace($array1,$array2, "abcdef"); //多对多的替换,输出"defdef"
?>
substr_replace
替换字符串的一部分。
6.字符串与HTML
略
7.其它字符串函数
1.字符串与数组
a.字符串转化为数组
explode()函数可以用指定的字符串分割另一个字符串,并返回一个数组
$str="使用 空格 分割 字符串";
array=explode(" ", $str);
pint_r($array);
输出Array ( [0] => 使用 [1] => 空格 [2] => 分割 [3] => 字符串 )
?>
b.数组转化为字符串
implode(string $glue,array $pieces)
$pieces是保存要连接的字符串的数组,$glue是用于连接字符串的连接符。例如:
$array=array("hello","how","are","you");
$str=implode(",",$array); //使用逗号作为连接符
echo $str; //输出"hello,how,are,you"
?>
c.字符串的加密函数
md5(); crypt(),但这个函数一旦加密后就无法转化为原来的形式。
4.3实例留言薄内容处理
一个留言簿,留言簿上有Email地址和用户的留言,提取客户的Email地址和留言,要求Email地址中@符号前不能有点“.”或逗号“,”。
将Email地址中@符号前的内容作为用户的用户名,并将用户留言中第一人称“我”修改为“本人”。
复制代码 代码如下:
if(isset($_POST['bt1']))
{
$Email=$_POST['Email']; //接收Eamil地址
$note=$_POST['note']; //接收留言
if(!$Email||!$note) //判断是否取得值
echo "<script>alert('Email地址和留言请填写完整!')</script>";
else
{
$array=explode("@", $Email); //分割Email地址
if(count($array)!=2) //如果有两个@符号则报错
echo "<script>alert('Email地址格式错误!')</script>";
else
{
$username=$array[0]; //取得@符号前的内容
$netname=$array[1]; //取得@符号后的内容
//如果username中含有“.”或“,”则报错
if(strstr($username,".") or strstr($username,","))
echo "<script>alert('Email地址格式错误!')</script>";
else
{
$str1= htmlspecialchars(" $str2= htmlspecialchars(">"); //输出符号“>”
//将留言中的“我”用“本人”替代
$newnote=str_replace("我","本人",$note);
echo "";
echo "用户". $str1. $username . $str2. "您好! ";
echo "您是". $netname. "网友!
";
echo "
您的留言是:
".$newnote."
";
echo "";
}
}
}
}
?>
函数原型:array explode(string separator,string input);
explode函数应用非常广泛,其主要作用是对规定的字符串以设定的分隔符进行拆分,并以数组形式返回。其常使用在分割文件名以判断文件类型、切割用户Email等场合。
PHP字符串分割函数explode处理实例
1、获取文件扩展名
$fileName = "leaps教程oulcn.jpg";
$str = explode(".",$fileName);
print_r($str);
我们知道在PHP文件上传功能中,判断上传文件名是否合法的最基本方法是判断扩展名是否合法,这时候就需要使用PHP字符串函数explode对文件名进行分割处理。在上述代码中explode函数以.为分隔符,对文件名进行分割。输入结果如下
Array ( [0] => leapsoulcn [1] => jpg )
2、获取用户Email域名信息
$emailInfo = explode("@",$email);
手册
AddSlashes: 字符串加入斜线。
bin2hex: 二进位转成十六进位。
Chop: 去除连续空白。
Chr: 返回序数值的字符。
chunk_split: 将字符串分成小段。
convert_cyr_string: 转换古斯拉夫字符串成其它字符串。
crypt: 将字符串用 DES 编码加密。
echo: 输出字符串。
explode: 切开字符串。
flush: 清出输出缓冲区。
get_meta_tags: 抽出文件所有 meta 标记的资料。
htmlspecialchars: 将特殊字符转成 HTML 格式。
htmlentities: 将所有的字符都转成 HTML 字符串。
implode: 将数组变成字符串。
join: 将数组变成字符串。
ltrim: 去除连续空白。
md5: 计算字符串的 MD5 哈稀。
nl2br: 将换行字符转成
。
Ord: 返回字符的序数值。
parse_str: 解析 query 字符串成变量。
print: 输出字符串。
printf: 输出格式化字符串。
quoted_printable_decode: 将 qp 编码字符串转成 8 位字符串。
QuoteMeta: 加入引用符号。
rawurldecode: 从 URL 专用格式字符串还原成普通字符串。
rawurlencode: 将字符串编码成 URL 专用格式。
setlocale: 配置地域化信息。
similar_text: 计算字符串相似度。
soundex: 计算字符串的读音值
sprintf: 将字符串格式化。
strchr: 寻找第一个出现的字符。
strcmp: 字符串比较。
strcspn: 不同字符串的长度。
strip_tags: 去掉 HTML 及 PHP 的标记。
StripSlashes: 去掉反斜线字符。
strlen: 取得字符串长度。
strrpos: 寻找字符串中某字符最后出现处。
strpos: 寻找字符串中某字符最先出现处。
strrchr: 取得某字符最后出现处起的字符串。
strrev: 颠倒字符串。
strspn: 找出某字符串落在另一字符串遮罩的数目。
strstr: 返回字符串中某字符串开始处至结束的字符串。
strtok: 切开字符串。
strtolower: 字符串全转为小写。
strtoupper: 字符串全转为大写。
str_replace: 字符串取代。
strtr: 转换某些字符。
substr: 取部份字符串。
trim: 截去字符串首尾的空格。
ucfirst: 将字符串第一个字符改大写。
ucwords: 将字符串每个字第一个字母改大写。

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



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
