Ngôn ngữ PHP - Xóa từ cuối cùng của một chuỗi

Viết một script PHP để xóa từ cuối cùng khỏi một chuỗi.

Ví dụ: Nhập chuỗi "'The quick brown fox'"

PHP Regular Expression Exercise: Remove the last word from a string

Mã nguồn PHP:

<?php
$str1 = 'The quick brown fox';
echo preg_replace('/\W\w+\s*(\W*)$/', '$1', $str1)."\n";
?>

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: Remove the last word from a string