English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

在php裡面
substr()函數裡面
空格算不算字元?
還有此函數如何運算

可否幫我舉例一下 拜託!!

2005-12-18 15:43:41 · 2 個解答 · 發問者 Maxim 2 in 電腦與網際網路 程式設計

2 個解答

string substr ( string string, int start [, int length] )
這是substr的用法
substr("要拆解的字串",要從哪個位置開始拆解,拆解多少長度的量);

假如你指的空格在要拆解的字串裡面
那麼空格是算在substr的運作裡面
假如你指的是substr各欄位的空格,那麼其實是忽略的,但是適當的空格可以讓你的程式更美觀。

Example 1. Basic substr() usage
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achived using "curly braces"
$string = 'abcdef';
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f

?>

If start is negative, the returned string will start at the start'th character from the end of string.

Example 2. Using a negative start
$rest = substr("abcdef", -1); // returns "f"
$rest = substr("abcdef", -2); // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>

If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string). If string is less than or equal to start characters long, FALSE will be returned.

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned.

Example 3. Using a negative length
$rest = substr("abcdef", 0, -1); // returns "abcde"
$rest = substr("abcdef", 2, -1); // returns "cde"
$rest = substr("abcdef", 4, -4); // returns ""
$rest = substr("abcdef", -3, -1); // returns "de"
?>

2005-12-18 18:49:29 · answer #1 · answered by 魚兒來 6 · 0 0

算啊xyz都是啊如substr[a]=0.......以此類推

2005-12-18 16:55:09 · answer #2 · answered by 崇仁 張 3 · 0 0

fedest.com, questions and answers