关于PHP字符串的基础知识总结_PHP
字符串
注意单引号和双引号的区别
注意转义字符\的使用\\,\",\$
注意使用8进制或16进制字符表示 \xf6
echo "H\xf6me";//需要察看是否支持此类文字编码<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> echo </font><font color="#DD0000">"H\xf6me\";//需要察看是否支持此类文字编码<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
1.使用printf()和sprintf()创建格式化的输出
printf()直接输出到输出缓冲区
sprintf()的输出作为字符串返回
如printf("输出内容 %.2f\n",$PI());
所有的转换规范都以%开头
数据类型有d-整数,s-字符串,f-浮点数,b-二进制
.2是一个可选的宽度指标,小数点右边输出使用0填充
printf("%.2f",3.14159);
printf("%10.2f",3.14159);
printf("%.10f",3.14159);
printf("%.9s",abcdefghijklmn);
printf("%5.2f,%f,%7.3f\m",3.14159,3.14159,3.14159);
printf("%b %d %f %s \n",123,123,123,"test");
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"%.2f\",3.14159);<br><br> printf(\"%10.2f\",3.14159);<br><br> printf(\"%.10f\",3.14159);<br><br> printf(\"%.9s\",abcdefghijklmn);<br><br> printf(\"%5.2f,%f,%7.3f\m\",3.14159,3.14159,3.14159);<br><br> printf(\"%b %d %f %s \n\",123,123,123,\"test\");<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
2.字符串填充
string str_pad(string input原始字串, int length添加后的总长度[, string padding要填充的字符 [, int pad_type]填充类型])
填充类型有添加在左边STR_PAD_LEFT,默认添在右边,填充在两端STR_PAD_BOTH
$index = array("one"=>1,"two"=>155,"three"=>1679);
echo "
";<br> echo str_pad("这是标题",50," ",STR_PAD_BOTH)."\n";<br> foreach($index as $inkey=>$inval)<br> echo str_pad($inkey,30,".").str_pad($inval,20,".",STR_PAD_LEFT)."\n";<br> echo "
";
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">$index </font><font color="#007700">= array(</font><font color="#DD0000">"one\"=>1,\"two\"=>155,\"three\"=>1679);<br><br> echo \"<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">\";<br><br> echo str_pad(\"这是标题\",50,\" \",STR_PAD_BOTH).\"\n\";<br><br> foreach($index as $inkey=>$inval)<br><br> echo str_pad($inkey,30,\".\").str_pad($inval,20,\".\",STR_PAD_LEFT).\"\n\";<br><br> echo \"</pre><div class="contentsignin">Copy after login</div></div>\";<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
string strtolower(string subject)//转换为小写
string strtoupper(string subject)//转换为大写
string ucfirst(string subject)//首字母大写
string ucwords(string subject)//每个单词首字母大写
string ltrim(string subject)//去左空白
string rtrim(string subject)//去右空白
string trim(string subject)去左右空白,空白包括null,制表符,换行符,回车符和空格
string n12br(string source)//将\n表示的换行符转换为
标记
3.字符串比较
integer strcmp(sting str1,string str2) //str1大于str2返回-1 str1小于str2返回1 str1和str2相等返回0
integer strmcmp(sting str1,string str2,integer length) //第三个参数限制length个字符的比较
print strcmp("aardvark","aardwolf");
print strncmp("aardvark","aardwolf",4);<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> print </font><font color="#0000BB">strcmp</font><font color="#007700">(</font><font color="#DD0000">"aardvark\",\"aardwolf\");<br><br> print strncmp(\"aardvark\",\"aardwolf\",4);<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
strcasecmp()和strncasecmp()是不区分大小写的比较函数
4.查找和抽取子字符串
string substr(sting source,integer start[,integer length])//从start开始取length个字符
start和length可以使用负值
$var = "abcdefgh";
print substr($var,2);//从0开始计数
print substr($var,2,3);
print substr($var,-1);//从字符串的末尾开始
print substr($var,-5,2);
print substr($var,-5,-2);<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">$var </font><font color="#007700">= </font><font color="#DD0000">"abcdefgh\";<br><br> print substr($var,2).\"\";<br><br> print substr($var,2,3).\"\";<br><br> print substr($var,-1).\"\";<br><br> print substr($var,-5,2).\"\";<br><br> print substr($var,-5,-2).\"\";<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
integer strpos(string haystack,string needle[,integer offset])//查找子字符串的位置,返回第一次出现.
integer strrpos(string haystack,string needle)//只搜索单个字符(多个字符只取第一个),返回最后一次出现的索引.
还有常见的从 字符串中抽取找到的部分 的函数
string strstr(string haystack,string needle)//不区分大小写
string stristr(string haystack,string needle)//区分大小写
string strrchr(string haystack,sting needle)
***********
array explode(string separator,string subject[,integer limit])//返回一个字符串数组
array implode(string glue,array pieces)//返回一个字符串
///////////////////////////代码段////////////////////////////////////////
$guest = "this is a string";
$guestArray = explode(" ",$guest);
var_dump($guestArray);
sort($guestArray);
echo implode(",",$guestArray);
////////////////////////////////////////////////////////////////////////
<font color="#000000">
<font color="#0000BB"></font><font color="#007700"></font><font color="#0000BB">php<br><br> </font><font color="#007700">echo </font><font color="#DD0000">'---------------------输出结果----------------------------------------'</font><font color="#007700">;<br><br> </font><font color="#0000BB">$guest </font><font color="#007700">= </font><font color="#DD0000">"this is a string\";<br><br> $guestArray = explode(\" \",$guest);<br><br> var_dump($guestArray);<br><br> sort($guestArray);<br><br> echo implode(\",\",$guestArray);<br><br> echo '---------------------------------------------------------------------';<br><br> ?></font>
</font>
5.替换字符和子字符串
string substr_replace(string source,string replace,int start[,int length])

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



Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat
