PHP學習筆記11(字串函式)
阿新 • • 發佈:2019-01-03
(1)字串定界符
- 單雙引號
<?php
//單引號(不會解析變數)
$a = 'Hello';
//雙引號(會解析變數)
$b = "Hello";
- Heredoc(會解析變數)
//Heredoc
$username = 'tom';
$c = <<<GOD
Hello <br/>
{$username}
GOD;
echo $c;
- Nowdoc(不解析變數)
<?php
$username = 'tom';
$str = <<<'EOD'
hello king<br/>
{$username }
EOD;
echo $str;
- {}使用
//{}
echo "我的名字$a";//正常
echo "我的名字$as";//解析失敗
echo "我的名字{$a}s";//我的名字Hellos
$str = 'abcdef';
echo $str{1};
$str{0} = 'hello';//只能替換一個字元
echo $str;
$str{1}='';//刪除,只是看不到,長度還在
echo $str;
$str{6}='e';//新增
echo $str;
(2)其他型別轉換成字串
- 自動轉換
<?php
//陣列-》字串
echo 123;
echo "<br/>";
//布林-》字串
echo 'A',false,'B';//false輸出為null
echo "<br/>";
echo 'A',null,'B';
echo "<br/>";
echo 'A',true,'B';//true輸出為1
echo "<br/>";
//陣列不能直接轉換成字串
$arr = array(12,3,4);
echo $arr;//報錯 Notice: Array to string conversion
//物件不能自動轉換成字串
class Person{
public $name;
}
echo new Person();//Catchable fatal error: Object of class Person could not be converted to string
- 強制型別轉換
<?php
/**
* 臨時轉換型別
*/
//數值強轉字串
$var=123;
$res =(string)$var;
var_dump($res);//string
var_dump($var);//int
//物件不能強轉為字串
class Person{
public $name;
}
$p = new Person();
echo (string)$p;//Catchable fatal error: Object of class Person could not be converted to string
/**
* 永久轉換型別
*/
$str = 123;
echo gettype($str);//integer
settype($str,'string');
echo gettype($str);//string
(3)字串轉換成其他型別
<?php
// 字串轉換成數值
// 去合法數字,如果不是轉換成0
echo 1+'3king';//4
echo 1.2+'4gff';//5.2
echo 3+'2e2';//203
echo 2+ 'true';//2
echo 2+ 'false';//2
// 字串轉換成布林值
$b = '';//假
$b = ' ';//真
$b = null;//假
$b = '0';//假
$b = '0.0';//真
if($b){
echo '真';
}else{
echo '假';
}
(4)字串函式庫
- 字串屬性
$str = 'hello World a child';
//檢測是否是字串
var_dump(is_string($str));//true
//字串長度
echo strlen($str)."<br/>";
//判斷是否為空
echo empty($str);
- 大小寫切換
<?php
$str = 'hello World a child';
//小寫轉換全部
echo strtolower($str)."<br/>";
//大寫轉換全部
echo strtoupper($str)."<br/>";
//第一個字母大寫
echo ucfirst($str)."<br/>";
//每個單詞的首字母大寫
echo ucwords($str)."<br/>";
- ASCII碼相關
$a = 'a';
//得到指定字元的ASCII
echo ord($a);
//根據ASCII返回指定字元
echo chr(97);
- 擷取substr()
//string substr ( string $string , int $start [, int $length ] )
$rest = substr("abcdef", -1); // returns "f"
$rest = substr("abcdef", -2); // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
$rest = substr("abcdef", 0, -1); // returns "abcde"
$rest = substr("abcdef", 2, -1); // returns "cde"
$rest = substr("abcdef", 4, -4); // returns false
$rest = substr("abcdef", -3, -1); // returns "de"
- strcmp() strcasecmp
// int strcmp ( string $str1 , string $str2 )
// Returns < 0 if str1 is less than str2;
// > 0 if str1 is greater than str2,
// and 0 if they are equal.
//比較字串大小,區分大小寫
echo strcmp('A','a');//-1
//比較字串大小,不區分大小寫
echo strcasecmp('A','a');//0
- strpos()字串首次出現的位置(區分大小寫)
- stripos()不區分大小寫
- strrpos()字串最後出現的位置(區分大小寫)
- strripos不區分大小寫
//mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
//mixed stripos ( string $haystack , mixed $needle [, int $offset = 0 ] )
如果不存在返回fasle
- 拆分
//array explode ( string $delimiter , string $string [, int $limit ] )
<?php
$s = 'abc1efd1dfd1';
var_dump(explode('1',$s,2));
// array (size=2)
// 0 => string 'abc' (length=3)
// 1 => string 'efd1dfd1' (length=8)
- 加密
echo md5("king");//輸出32位加密字串
echo sha1('king');//輸出40位加密字串