初学PHP,因为曾经有一点PHP的基础,决定选择PHP cookbook 这本书,了解一下PHP的各种应用,将一些零碎的知识点记在博客上。
0x01 字符串操作函数
mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) 返回 needle 在 haystack 中首次出现的数字位置。 也可用于判断 needle是否存在于haystack中 例如 if(strpos("hacking for fun ","hack")===false){ return "hack" //一定要用三个等号 }
2, substr()
substr("hello world ",$start ,$length) //抽取语句 substr_replace("hello world","php",$start,$length) //替换语句 strstr ( $haystack , $needle )//返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。
3,lookandsay.php 输出lookandsay序列 这是一段很有意思的数字,前一行读出来就是第二行
<?php function lookandsay($s){ $r = "";//输出 $m = $s[0];//要找的数 $n= 1;//计数 for($i=1,$j=strlen($s);$i<$j;$i++){ if($m==$s[$i]) { $n++ ;} else{ $r.=$n.$m; $n=1; $m=$s[$i]; } } return $r.$n.$m; } for($i=0,$s=1;$i<10;$i++){ $s=lookandsay($s); print "$s <br/>\n"; } ?> 1 11 //一个1 21 //两个1 1211 //一个2 一个1 1112211 312211 13112221
4,implode
string implode ( string $glue , array $pieces )//将一个一维数组的值转化为字符串 用$glue连接在一起,glue可以为空。 如果想分割bool数组,例如 $array = array(true,false,true,false,true,true); $s=implode('',$array); 输出结果false会消失,=> 只有 11115,stripslashes 去除转义反斜线 \\ 双反斜线被转义为一个
string stripslashes ( string $str ) 官方例程 递归转义数组 <?php function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } // 范例 $array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")); $array = stripslashes_deep($array); // 输出 print_r($array); ?>
6,字符串中插入表达式或变量
print "You have pay ".$REQUEST['payment']."money";字符串连接 print "You own ".$amount['payment']."immediately"; print "You own $amount[payment] immediately";//在字符串之中只能用$amount[payment]这种形式 可以将它放在大括号之中 print "You own {$amount['payment']}immediately";7,trim函数
print trim(" something space \t\r\0\x0B\n","spa").'test01'; print trim('there are w0rds;3','0..9'); print trim('there are w0rds;3','the'); 经测试trim函数可以删除特定的字符,但是要求要删除部分在字符串两边。
————————————————这里是更新的分割线————————————————————
最后的编程练习:可下载的csv文件
留坑 晚上添
转载于:https://www.cnblogs.com/moonnight/p/5412457.html