Substring php

Parameters

string The input string.

offmix

If offset is non-negative sầu, the returned string will start at the offset'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', và so forth.

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

If string is less than offphối characters long, false will be returned.


(obatambeienwasirherbal.com.. 4, obatambeienwasirherbal.com 5, obatambeienwasirherbal.com 7, obatambeienwasirherbal.com 8)

substr — Return part of a string


$rest substr("abcdef", -1);    // returns "f"
$rest substr("abcdef", -2);    // returns "ef"
$rest substr("abcdef", -31); // returns "d"
?>

length

If length is given và is positive sầu, the string returned will contain at most length characters beginning from offmix (depending on the length of string).

If length is given and is negative, then that many characters will be omitted from the over of string (after the start position has been calculated when a offset is negative). If offphối denotes the position of this truncation or beyond, false will be returned.

If length is given and is 0, false or null, an empty string will be returned.

If length is omitted, the substring starting from offphối until the end of the string will be returned.


(obatambeienwasirherbal.com 4, obatambeienwasirherbal.comhường. 5, obatambeienwasirherbal.com. 7, obatambeienwasirherbal.com 8)

substr — Return part of a string


$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"
?>

Return Values

Returns the extracted part of string; or false on failure, or an empty string.


Changelog

Version Description 8.0.0 length is nullable now. 8.0.0 The function returns an empty string where it previously returned false.

(obatambeienwasirherbal.comhường 4, obatambeienwasirherbal.com 5, obatambeienwasirherbal.comhường 7, obatambeienwasirherbal.com 8)

substr — Return part of a string


(obatambeienwasirherbal.com 4, obatambeienwasirherbal.comhường. 5, obatambeienwasirherbal.com 7, obatambeienwasirherbal.com 8)

substr — Return part of a string


echo substr("abcdef"1);     // bcdef
echo substr("abcdef"13);  // bcd
echo substr("abcdef"04);  // abcd
echo substr("abcdef"08);  // abcdef
echo substr("abcdef", -11); // f

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string "abcdef";
echo 
$string<0>;                 // a
echo $string<3>;                 // d
echo $string<strlen($string)-1>; // f

?>

Example #4 substr() casting behaviour


class apple 
}

echo 
"1) ".

Bạn đang xem: Substring php

var_export(substr("pear"02), true).obatambeienwasirherbal.com_EOL;
echo 
"2) ".var_export(substr(5432102), true).obatambeienwasirherbal.com_EOL;
echo 
"3) ".var_export(substr(new apple(), 02), true).obatambeienwasirherbal.com_EOL;
echo 
"4) ".

Xem thêm: Độc Đáo Thịt Xiên Nướng Sài Gòn "Tuyệt Cú Mèo", 15 Quán Nướng Ngon Ở Sài Gòn Say Đắm Lòng Người

var_export(substr(true01), true).obatambeienwasirherbal.com_EOL;
echo 
"5) ".var_export(substr(false01), true).obatambeienwasirherbal.com_EOL;
echo 
"6) ".var_export(substr(""01), true).obatambeienwasirherbal.com_EOL;
echo 
"7) ".var_export(substr(1.2e304), true).obatambeienwasirherbal.com_EOL;
?>

đầu ra of the above sầu example in obatambeienwasirherbal.comhường 7:


1) 'pe' 2) '54' 3) 'gr' 4) '1' 5) '' 6) '' 7) '1200'

đầu ra of the above sầu example in obatambeienwasirherbal.com 5:


1) 'pe' 2) '54' 3) 'gr' 4) '1' 5) false 6) false 7) '1200'

Errors/Exceptions

Returns false on error.
var_dump(substr("a"2)); // bool(false)
?>

See Also

strrchr() - Find the last occurrence of a character in a string substr_replace() - Replace text within a portion of a string preg_match() - Persize a regular expression match trim() - Strip whitespace (or other characters) from the beginning & over of a string mb_substr() - Get part of string wordwrap() - Wraps a string to lớn a given number of characters String access và modification by character