PHP lis () function
contoh
Lepaskan sisi kiri dari string karakter ( "Hello" dalam "Dia" dan "World" di "d"):
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
Menjalankan contoh »
Definisi dan Penggunaan
trim () fungsi menghapus kedua sisi dari string karakter kosong atau karakter yang telah ditetapkan lainnya.
fungsi yang terkait:
- LTRIM () - Lepaskan sisi kiri dari string karakter kosong atau karakter yang telah ditetapkan lainnya.
- RTRIM () - menghapus hak string karakter kosong atau karakter yang telah ditetapkan lainnya.
tatabahasa
trim( string,charlist )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
charlist | 可选。规定从字符串中删除哪些字符。如果省略该参数,则移除下列所有字符:
|
rincian teknis
Pengembalian: | Mengembalikan string dimodifikasi. |
---|---|
Versi PHP: | 4+ |
Update log: | Dalam PHP 4.1, tambahkan parameter charlist. |
contoh yang lebih
contoh 1
Hapus string ruang di kedua sisi:
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
Output HTML dari kode di atas adalah sebagai berikut (lihat sumber):
<!DOCTYPE html>
<html>
<body>
Without trim: Hello World! <br>With trim: Hello World!
</body>
</html>
<html>
<body>
Without trim: Hello World! <br>With trim: Hello World!
</body>
</html>
Output browser kode di atas adalah sebagai berikut:
Without trim: Hello World!
With trim: Hello World!
With trim: Hello World!
Menjalankan contoh »
contoh 2
Hapus string di kedua sisi jeda baris (\ n):
<?php
$str = "nnnHello World!nnn";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
$str = "nnnHello World!nnn";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
Output HTML dari kode di atas adalah sebagai berikut (lihat sumber):
<!DOCTYPE html>
<html>
<body>
Without trim:
Hello World!
<br>With trim: Hello World!
</body>
</html>
<html>
<body>
Without trim:
Hello World!
<br>With trim: Hello World!
</body>
</html>
Output browser kode di atas adalah sebagai berikut:
Without trim: Hello World!
With trim: Hello World!
With trim: Hello World!
Menjalankan contoh »
PHP String Reference