


Summary of examples of commonly used string manipulation functions in PHP trim, nl2br, addcslashes, uudecode, md5, etc.
本文实例总结了PHP常用字符串操作函数。分享给大家供大家参考,具体如下:
/*常用的字符串输出函数 * * echo() 输出字符串 * print() 输出一个或多个字符串 * die() 输出一条信息,并退出当前脚本 * printf() 输出格式化字符串 * sprintf() 把格式化的字符串写入到一个变量中 * */ //ucfirst //将字符串中的首字母转换为大写 $str="string"; echo ucfirst($str); echo "[object Object] "; //ucwords() //将字符串中的每个单词的首字母大写 $ucword="hello everyone!"; echo ucwords($ucword); echo "[object Object] "; //ltrim() rtrim() trim() //去除空格 $str="123 This is a test....."; echo ltrim($str,"0..9")." "; //去除左侧的数字 echo rtrim($str,".")." "; echo trim($str,"0..9A..Z.")." "; //去除字符串两端的大写字母,数字还有. //HTML相关的字符串格式化函数 //nl2br() //将字符串中的\n转换为" " $str="this is \n hello world"; echo nl2br($str).' '; //htmlspecialchars() //将html标记以字符的形式显示,不进行解释 $str=""; echo $str." "; echo htmlspecialchars($str); echo "[object Object] "; //addcslashes //添加反斜线 $str=addcslashes("foo[]","A..z"); echo $str." "; echo addcslashes("zoo['.']",'A..z')." "; //convert_uuencode() //利用uudecode的方法对字符串进行编码 $string="hello world"; $str= convert_uuencode($string); echo $str." "; echo convert_uudecode($str)." "; //html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' ]] ) //与htmlentities方法相反,将进行编码后的html字符转换为浏览器能够编译的形式 $a="I want a bright "; $b= htmlentities($a)." "; echo $b; echo html_entity_decode($b); echo "[object Object] "; //htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] ) //与htmlspecialchars函数相反,将HTML实体转换为字符 $c=htmlspecialchars($a); echo $c." "; echo htmlspecialchars_decode($c)." "; echo "[object Object] "; //lcfirst ( string $str ) //将字符串的首字符小写 $str="Hello World"; // echo lcfirst($str)." "; //md5_file ( string $filename [, bool $raw_output = false ] ) //对文件进行md5加密 // $string="password"; $str=md5($string); if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){ echo "The password is right "; } //parse_str ( string $str [, array &$arr ] ) //将一个字符串进行解析,解析成变量和数组的形式 $str = "first=value&arr[]=foo+bar&arr[]=baz"; parse_str($str,$input); print_r($input); echo "[object Object] "; //string sha1_file ( string $filename [, bool $raw_output = false ] ) //计算文件的散列值 foreach(glob("C:/lamp/appache2/htdocs/*.php") as $ent){ if(is_dir($ent)){ continue; } echo $ent."(SHA1:".sha1_file($ent).") "; } echo "[object Object] "; //int similar_text ( string $first , string $second [, float &$percent ] ) //计算两个字符串的相似度,通过引用方式传递第三个参数,similar_text() 将 //计算相似程度百分数。 $string1="rogerzhalili"; $string2="zhangjieroger"; if(similar_text($string1,$string2,$percent)){ echo $string1." and ".$string2." has the similarity of:".$percent." "; } echo "[object Object] "; //string str_shuffle ( string $str ) //打乱一个字符串 $string="I want you to solve this problem"; echo str_shuffle($string)." "; //array str_split ( string $string [, int $split_length = 1 ] ) //按照指定的长度对字符串进行分割 $arr=str_split($string,3); //str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ) //统计字符串中单词的数量 echo "[object Object] "; //int strripos ( string $haystack , string $needle [, int $offset = 0 ] ) //以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位 //置。与 strrpos() 不同,strripos() 不区分大小写。 //offset用于指定从那个位置开始查找 $haystack='ababcd'; $needle='Ab'; echo "the last".$needle."postion is:".strripos($haystack,$needle)." "; echo strrpos($haystack,'ab'); echo "[object Object] "; //string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) //返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结 //尾的字符串。 该函数区分大小写。如果想要不区分大小写,请使用 //stristr()。 $a="the First test"; $needle="Fi"; echo strstr($a,$needle)." "; if($c=strstr($a,"Fio")){ echo "find".$c." "; } else { echo "not find the string! "; } echo "[object Object] "; //int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) //查找$needle子字符串在$haystack中出现的次数,$needle区分大小写 $hay="la la wa la wa wa lala"; echo substr_count($hay,"la")." "; //int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] ) //正则匹配,将匹配后的结果存放到$matches(如果指定了$matches的话) preg_match_all("/?(\d3)?? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212", $phones); echo ""; echo "[object Object] "; //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) //搜索subject中匹配pattern的部分, 以replacement进行替换. $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}1,$3'; echo preg_replace($pattern,$replacement,$string); echo "[object Object] "; //array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] ) //通过一个正则表达式分隔给定字符串. $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars);
更多关于PHP字符串操作相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结》
希望本文所述对大家PHP程序设计有所帮助。
以上就介绍了PHP常用字符串操作函数实例总结trim、nl2br、addcslashes、uudecode、md5等,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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











What is MD5? MD5 Message-DigestAgorithm (English: MD5Message-DigestAgorithm), a widely used cryptographic hash function, can produce a 128-bit (16-byte) hash value (hash value) to ensure complete and consistent information transmission. MD5 was designed by American cryptographer Ronald Linn Rivest and made public in 1992 to replace the MD4 algorithm. The program of this algorithm is specified in the RFC1321 standard. After 1996, the algorithm was proven to have weaknesses and could be cracked. For data requiring high security, experts generally recommend using other algorithms.

This article will explain in detail about PHP calculating the MD5 hash of files. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP calculates the MD5 hash of a file MD5 (MessageDigest5) is a one-way encryption algorithm that converts messages of arbitrary length into a fixed-length 128-bit hash value. It is widely used to ensure file integrity, verify data authenticity and create digital signatures. Calculating the MD5 hash of a file in PHP PHP provides multiple methods to calculate the MD5 hash of a file: Use the md5_file() function. The md5_file() function directly calculates the MD5 hash value of the file and returns a 32-character

In PHP programming, spaces are often encountered when processing strings, including Chinese spaces. In actual development, we often use the trim function to remove spaces at both ends of a string, but the processing of Chinese spaces is relatively complicated. This article will introduce how to use the trim function in PHP to process Chinese spaces and provide specific code examples. First, let us understand the types of Chinese spaces. In Chinese, spaces include not only common English spaces (space), but also some other special spaces.

The trim function of the solid-state drive is mainly to optimize the solid-state drive, solve the problem of slowdown and lifespan of the SSD after use, and improve the efficiency of the SSD by preparing data blocks for reuse. The Trim function is a function that almost all SSD solid state drives have. It is an ATA command. When the system confirms that the SSD supports Trim and deletes data, it does not notify the hard disk of the deletion command and only uses the Volume Bitmap to remember that the data here has been deleted. This enables faster data processing.

This article will explain in detail how PHP calculates the MD5 hash value of a string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Calculating the MD5 hash value of a string in PHP Introduction MD5 (Message Digest 5) is a popular cryptographic hash function used to generate fixed-length hash values, often used to protect data integrity, verify file integrity and Create a digital signature. This article will guide PHP developers on how to use built-in functions to calculate the MD5 hash value of a string. md5() function PHP provides the md5() function to calculate the MD5 hash value of a string. This function receives a string parameter and returns a 32-character hexadecimal hash value.

Use the strings.Trim function to remove the specified character set at the beginning and end of a string. In the Go language, the strings.Trim function is a very practical function that can remove the specified character set at the beginning and end of a string, making the string more tidy and standardized. This article will introduce how to use the strings.Trim function and show some code examples. First, let’s take a look at the prototype of the strings.Trim function: funcTrim(sstring,cutsetstri

The PHP function "trim" is a very useful string processing function. It can help us remove whitespace characters at both ends of the string, including spaces, tabs, newlines, etc. When writing PHP programs, we often encounter situations where user input needs to be cleaned. At this time, using "trim" can ensure that we get a clean string and avoid errors caused by irregular user input. Here is a simple code example showing how to use the "trim" function: <?php

Using an SSD drive leaves you with the constant worry of losing your data and being unable to recover it. However, Windows allows you to achieve optimal performance by executing TRIM commands that write only necessary data without having to manage old data blocks. To do this, you need to make sure your SSD supports TRIM and enable it in your operating system. How to check if TRIM is enabled? In most cases, TRIM functionality is enabled by default in modern SSDs. But to make sure this is checked out, you can run the command with administrative rights. Just open an elevated command prompt, run the fsutil behavioral query DisableDeleteNotify command and your SSD will be listed. 0 means enabled, 1 means disabled. like
