substr()函數介紹
substr() 函數返回字串的一部分。
文法:substr(string,start,length)。
- string:必需。規定要返回其中一部分的字串。
- start:必需。規定在字串的何處開始。正數 - 在字串的指定位置開始;負數 - 在從字串結尾的指定位置開始;0 - 在字串中的第一個字元處開始。
- charlist:可選。規定要返回的字串長度。預設是直到字串的結尾。正數 - 從 start 參數所在的位置返回;負數 - 從字串末端返回。
注釋:如果 start 是負數且 length 小於等於 start,則 length 為 0。
Program List:負值的start參數
';$rest = substr("abcdef", -2); // returns "ef"echo $rest.'
';$rest = substr("abcdef", -3, 1); // returns "d"echo $rest.'
';?>
程式運行結果:
fefd
Program List:負值的length參數
就是從start位置開始,若length為負值的話,就從字串的末尾開始數。substr("abcdef", 2, -1)的話,就是從c開始,然後-1說明截取到e,就是要截取cde。
';$rest = substr("abcdef", 2, -1); // returns "cde"echo $rest.'
';$rest = substr("abcdef", 4, -4); // returns ""echo $rest.'
';$rest = substr("abcdef", -3, -1); // returns "de"echo $rest.'
';?>
程式運行結果:
abcdecdede
Program List:基本的substr()函數用法
';echo substr('abcdef', 1, 3); // bcdecho '
';echo substr('abcdef', 0, 4); // abcdecho '
';echo substr('abcdef', 0, 8); // abcdefecho '
';echo substr('abcdef', -1, 1); // fecho '
';// Accessing single characters in a string// can also be achieved using "square brackets"$string = 'abcdef';echo $string[0]; // aecho '
';echo $string[3]; // decho '
';echo $string[strlen($string)-1]; // fecho '
';?>
程式運行結果:
bcdefbcdabcdabcdeffadf
Program List:移除尾碼
程式運行結果:
bkjia.jpg
Program List:太長的字串只顯示首尾,中間用省略符號代替
30) { $vartypesf = strrchr($file,"."); // 擷取字元創總長度$vartypesf_len = strlen($vartypesf); // 截取左邊15個字元$word_l_w = substr($file,0,15); // 截取右邊15個字元$word_r_w = substr($file,-15); $word_r_a = substr($word_r_w,0,-$vartypesf_len); return $word_l_w."...".$word_r_a.$vartypesf; } else return $file; } // RETURN: Hellothisfileha...andthisfayl.exe $result = funclongwords($file);echo $result;?>
程式運行結果:
Hellothisfileha...andthisfayl.exe
Program List:將多出的文字顯示為省略符號
很多時候我們需要顯示固定的字數,多出的字數用省略符號代替。
$length) return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer; return $string; } ?>
程式運行結果:
welcome to bkjia, I hope...
Program List:格式化字串
有時候我們需要格式化字串,比如電話號碼。
= $FormatPos){ //If its a number => stores it if (is_numeric(substr($Format, $FormatPos, 1))){ $Result .= substr($String, $StringPos, 1); $StringPos++; //If it is not a number => stores the caracter } else { $Result .= substr($Format, $FormatPos, 1); } //Next caracter at the mask. $FormatPos++; } return $Result;}// For phone numbers at Buenos Aires, Argentina// Example 1: $String = "8607562337788"; $Format = "+00 0000 0000000"; echo str_format_number($String, $Format); echo '
';// Example 2: $String = "8607562337788"; $Format = "+00 0000 00.0000000"; echo str_format_number($String, $Format); echo '
';// Example 3: $String = "8607562337788"; $Format = "+00 0000 00.000 a"; echo str_format_number($String, $Format); echo '
';?>
程式運行結果:
+86 0756 2337788+86 0756 23.37788+86 0756 23.377 a
http://www.bkjia.com/PHPjc/752408.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752408.htmlTechArticlesubstr()函數介紹 substr() 函數返回字串的一部分。 文法:substr(string,start,length)。 string:必需。規定要返回其中一部分的字串。 start:必...