字符串操作
1.手動轉移字符串數據
<?php?$name='Garfield';?$str1='The'.$name.'?cat\'s?is?pretty?\n';?echo?$str1;?$str2="This?is?one?line.\nAnd?this's?another?line.";?echo?$str2;?$var=123;?$str3="The?\$var?is?$var";?echo?$str3;??>?
2.自動轉移字符串數據
addslashes()
作用是為字符串里面的部分字符添加反斜線轉義字符,addslashes()只為4個字符添
加轉義,包括:單引號“’”,雙引號“””,反斜線“\”和NULL(“\0”)
<?php?$str="[sql]Who's?Raymond?";?echo?$str?.?"This?is?not?safe?in?a?database?query.<br/>";?echo?addslashes($str)?.?"This?is?safe?in?a?database?query."."<br/>";??>?
addcslashes()
格式:addcslashes(<string>,characters)
類似與使用反斜線轉義字符串中的字符
<?php?$str="Hello?Friend,Never?give?up!";?echo?$str."<br/>";?echo?addcslashes($str,?'g')."<br/>";?echo?addcslashes($str,?'a..g')."<br/>";??>?
3.數值轉換字符串
chr()
格式:char($var)
根據$var中的ASCII值返回相對應的字符,因此$var變量必須是一個合法的ASCII碼
值
<?php?$var=98;?echo?chr($var);??>?
ord()
格式:ord($string)
返回$string字符串首字符的ASCII碼值
<?php?$str="hello";?if?(ord($str)==10){?????echo?"\$str第一個字符的ASCII碼是10.\n";?}else{?????echo?"\$str第一個字符的ASCII碼是".ord($str);?}??>
4.字符串序列化
serialize()函數用于將數據集轉換為可存儲在數據塊或回話記錄中的字符串(如
SESSION或cookie的字符串)
unserialize()是反序列化
<?php?$s='php';?echo?serialize($s);??>?----------------------?<?php?$arry=array("cats","drop","fur","everywhere");?echo?"原數組內容:<pre>";?print_r($arry);?echo?"<pre>序列化后的數組:<br/>";?$string2=serialize($arry);?print_r($string2);?echo?"</pre>反序列化后的數組:<br/><pre>";?$arry2=unserialize($string2);?print_r($arry2);?echo?"</pre>";??>?
5.清理字符串中的空格
trim()? 去除不必要的空格、換行符、水瓶和垂直制表符以及字符串結束符
ltrim() 去除字符串變量左側的空格和格式控制符
rtrim() 去除字符串變量右側的空格和格式控制符
6.格式化字符串
nl2br()與wordwrap()
這兩個函數提供的都是文本或字符串換行功能
nl2br()是將字符串中的\n換行符轉換為html中的<br>標簽。經常用于用戶提交表單
文本內容,經nl2br()處理換行后插入數據到數據表中,或從數據表中提取后,用它
轉換顯示一個標準的HTML換行的內容
wordwrap函數用于指定從某列字符進行強制轉換
<?php?$string="Hello\nWorld\n\nHow?are?you?";?echo?nl2br($string)."<br>";??$textblock="See?spot?run,?run?spot?run.?See?spot?roll,?run?spot?roll!";?echo?wordwrap($textblock,20,"<br/>");??>?
7.改變字符串的大小寫
<?php?$astring="heloo?world";?echo?strtolower($astring);?echo?strtoupper($astring);?echo?ucfirst($astring);?echo?ucwords($astring);??>?
8.字符串切分
格式:array explode(string $delimiter,string $string[,int $limit])
explode用戶切分一個有明顯標識的字符串,切分后返回一個索引數組
<?php?$email="webmster@php.net";?$mail_array=explode("@",$email);?print_r($mail_array);?$url_array=explode("/",?$_SERVER['REQUEST_URI']);?print_r($url_array);??>?
9.字符串截取
格式:string substr(string $string,int $start[,int $length])
substr()截取字符串
<?php?$test="Your?programing?ability?is?excellent";?echo?substr($test,?1)."<br>";?echo?substr($test,?-9)."<br>";?echo?substr($test,?0,4)."<br>";?echo?substr($test,?4,-13)."<br>";??>?
10.計算字符串的長度
格式:strlen<$string>/mb_strlen(<$string>);
計算一個字符串的長度
<?php?echo?strlen("China?is?great");??echo?strlen("偉大的中國");??>?
11.字符串單詞統計
格式:str_word_count($string)
統計一段文字中有多少個英語單詞
<?php?$text=<<<text?測試:programing?is?art!?wahaha?text;?$words=str_word_count($text);?echo?"單詞總數為:$words";??>?-------------------------------------?<?php?$text=<<<text?To?be?pure?PHP?developer,?PHP?is?simple,?PHP?is?good?web?script.?building?our?web?site.?text;?$words=str_word_count($text,2);?$count_array=array_count_values($words);??作統計后生成一個新數組?print_r($count_array);??>?
12.字符串查找
子串查找
格式:strstr($string,"keyword");?<?php?$string="this?is?a?demo?string";?if?(strstr($string,?"demo")){?????echo?"有該子串";?}else?{?????echo?"沒有該子串";?}??>?
查找子串的位置strpos()
格式: int strpos (string $haystack ,mixed $needle [,int $offset=0])
跟strstr()功能相似,只是返回的不是一個字符串,而是子串所出現的位置號
字符串重復str_repeat()
格式:str str_repeat(string $input,int $multiplier)
用于重復顯示一個字符或字符串
<?php?$repeat_str=str_repeat("PHP5",?10);?echo?$repeat_str."<br>";??>?
查找子串出現的次數substr_count()
格式:int substr_count($string,$keyword)
<?php?$repeat_str=str_repeat("PHP5",?10);?$counter=substr_count($repeat_str,?"PHP5");?echo?"字符串中PHP5字符一共出現".$counter."次.";??>?
13.字符串處理
字符串替換str_replace()
格式:mixed str_replace(mixed $search ,mixed $replace ,mixed $subject
[,int &$count])
str_replace()需要3個參數,第1個參數是要查找的目標關鍵字;第2個參數是要替
換的字符,可以是空白字符;第3個參數是搜索的目標文本。
<?php?$text_1="Welcome?to?Beijing";?$text_2="China";?$text_3=str_replace("Beijing",?$text_2,?$text_1);?echo?$text_3;??>?
字符串翻轉strrev()
<?php?$string="abcdefg12345";?echo?strrev($string);??>?
轉換HTML實體
在HTML中,有些字符有一些特殊的含義,比如小于號“<”是定義一個HTML標簽的開
始。假如想要瀏覽器顯示這些字符的話,需要在HTML代碼中插入實體。
一個字符實體(entities)包括三個部分:一個與符號(&),一個實體名或者一個實體號
,最后是一個分號(;),比如要在HTML文檔中顯示一個小于號,要這樣寫:"<",半角
空格為" ",&符號為"&"
在php中,htmlspecialchars()和htmlentities()函數是專門用來將字符轉換為HTML
實體的,一方面是為了頁面顯示安全,另一方面也是為了PHP代碼不易出錯
格式:string htmlspecialchars(string $string [,int $quote_style=ENT_COMPAT
[,string $charset [,bool $double_encode=true]]])
<?php?$str='<a?href="http://www.google.cn"?title="Goole?China">Goole?中國</a>';?echo?htmlspecialchars($str);??>?
htmlspecialchars()函數的第二個可選參數是選擇引號的轉換模式,可以選擇一下
三個常量
ENT_COMPAT:表示轉換雙引號是保留單引號
ENT_QUOTES:表示同時轉換雙引號和單引號
ENT_NOQUOTES:表示兩個都不轉換,默認為ENT_COMPAT
第三個參數表示鎖轉換的字符編碼集
清除HTML標簽strip_tags()
格式:string strip_tags(string[,allow])
<?php?$string="Email:<a?href='spam@21cto.com'>spam@21cto.com</a>";?echo?"原始字符串=".$string."<br>";?echo?"目錄字符串=".strip_tags($string)."<br>";??>?
字符串比較strcmp()與strncmp()
格式:int strcmp(string $str1,string $str2,)
???? int strncmp(string $str1,string $str2,int $len)
<?php?$stringone="my?story";?$stringtwo="my?story";?if?(strcmp($stringone,?$stringtwo)==0){?????echo?"兩個字符串相同。<br>";?}?if?(strncmp("php?developer",?"php",?3)==0){?????echo?"兩個字符前3個字符是相同的!";?}??>?
字符串解析strok()
格式:string strtok(string $str,string $token)
用于解析一個字符串,該函數會按照$token參數來解析和跟蹤原始字符串
<?php?$info="員工A:staff@21cto.com|思技創想,技術開發部";?$tokens=":|,";??$tokenized=strtok($info,?$tokens);?while?($tokenized){?????echo?"值=$tokenized<br>";??????????????個元素?????$tokenized=strtok($tokens);?}??>?
查找并替換字符串strpbrk()
格式:string strpbrk(string $haystack,string $char_list)
從原始字符串中的標識字符開始到截取到末尾,生成一個新的字符串
<?php?$text='Hello,Welcome?to?PHP5?world';?echo?strpbrk($text,?'W');?echo?"<br>";?echo?strpbrk($text,?'w');??>? ?
轉載于:https://blog.51cto.com/bighuamao/982899
總結
以上是生活随笔為你收集整理的PHP专题-开发基础(七)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。