Chuỗi PHP: Tập thể dục-7 với giải pháp
Viết tập lệnh PHP để có được ba ký tự cuối cùng của một chuỗi.
Chuỗi mẫu: '[Email & nbsp; được bảo vệ]'
Trình bày bằng hình ảnh:
Giải pháp mẫu:
Mã PHP:
Đầu ra mẫu:
com
Sơ đồ:
Trình chỉnh sửa mã PHP:
Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.
Trước đây: Viết tập lệnh PHP để trích xuất tên người dùng từ id email được chỉ định. Write a PHP script to extract the user name from the specified email ID.
Next: Write a PHP script to format values in
currency style.
PHP: Lời khuyên trong ngày
PHP: Sử dụng str_replace để nó chỉ hành động trong trận đấu đầu tiên?
Có thể được thực hiện với preg_replace:
function str_replace_first[$from, $to, $content] { $from = '/'.preg_quote[$from, '/'].'/'; return preg_replace[$from, $to, $content, 1]; } echo str_replace_first['abc', '123', 'abcdef abcdef abcdef']; // outputs '123def abcdef abcdef'
Phép thuật nằm trong tham số thứ tư tùy chọn [giới hạn]. Từ tài liệu:
[Giới hạn] - Sự thay thế tối đa có thể cho mỗi mẫu trong mỗi chuỗi chủ đề. Mặc định là -1 [không có giới hạn].
Tham khảo: //bit.ly/35Scup8
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : $str = "GeeksforGeeks!" $n = 6 Output : Geeks! Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks!
Bàn luận Tn this method, traverse the last N characters of the string and keep appending them in a new string.
Example:
6
function str_replace_first[$from, $to, $content] { $from = '/'.preg_quote[$from, '/'].'/'; return preg_replace[$from, $to, $content, 1]; } echo str_replace_first['abc', '123', 'abcdef abcdef abcdef']; // outputs '123def abcdef abcdef'5
com6
function str_replace_first[$from, $to, $content] { $from = '/'.preg_quote[$from, '/'].'/'; return preg_replace[$from, $to, $content, 1]; } echo str_replace_first['abc', '123', 'abcdef abcdef abcdef']; // outputs '123def abcdef abcdef'7
=
com3__
/** Unlike substr, handles case where $string is too short.
* @param $string
* @param $nChars - negative to return at end of string.
*/
function safe_substr[$string, $nChars] {
if [$nChars == 0 || !isset[$string]]
return "";
if [strlen[$string]
3function str_replace_first[$from, $to, $content] { $from = '/'.preg_quote[$from, '/'].'/'; return preg_replace[$from, $to, $content, 1]; } echo str_replace_first['abc', '123', 'abcdef abcdef abcdef']; // outputs '123def abcdef abcdef'75 Another way to do this use inbuilt library function substr with parameters as the name of the string.
Example:
7
function str_replace_first[$from, $to, $content]
{
$from = '/'.preg_quote[$from, '/'].'/';
return preg_replace[$from, $to, $content, 1];
}
echo str_replace_first['abc', '123', 'abcdef abcdef abcdef'];
// outputs '123def abcdef abcdef'
1com
0
/** Unlike substr, handles case where $string is too short.
* @param $string
* @param $nChars - negative to return at end of string.
*/
function safe_substr[$string, $nChars] {
if [$nChars == 0 || !isset[$string]]
return "";
if [strlen[$string]