文字列の両端から文字を削除します ("Hello" の "He" と "World" の "d!"):
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
例の実行 »
trim() 関数は、文字列の両端から空白またはその他の定義済み文字を削除します。
関連する関数:
trim(string,charlist)
パラメータ | 説明 |
---|---|
string | 必須。チェックする文字列を指定する |
charlist | 任意。文字列から削除する文字を指定する。省略した場合は、次の全ての文字が削除される:
|
返り値: | 変更された文字列を返します |
---|---|
PHP バージョン: | 4+ |
変更歴 | charlist パラメータは PHP 4.1 で追加されました |
文字列の両端から空白を削除します:
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
上のコードのHTML出力は次の通り(ソース表示):
<!DOCTYPE html>
<html>
<body>
Without trim: Hello World! <br>With trim: Hello World!
</body>
</html>
ブラウザへの上のコードの出力は、次の通り:
Without trim: Hello World!
With trim: Hello World!
例の実行 »
文字列の両端から改行 (\n) を削除します:
<?php
$str = "\n\n\nHello World!\n\n\n";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
上のコードのHTML出力は次の通り(ソース表示):
<!DOCTYPE html>
<html>
<body>
Without trim:
Hello World!
<br>With trim: Hello World!
</body>
</html>
ブラウザへの上のコードの出力は、次の通り:
Without trim: Hello World!
With trim: Hello World!
例の実行 »