وظيفة PHP trim()

مثال

إزالة الأحرف من الجانبين للنص ("Hello" من "He" وأيضًا "World" من "d!"):

<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>

Run Instances

التعريف والاستخدام

يحذف وظيفة trim() الأبيض من الجانبين للنص أو الأحرف المحددة مسبقًا

الوظائف ذات الصلة:

  • ltrim() - إزالة الأبيض من بداية النص أو الأحرف المحددة مسبقًا
  • rtrim() - إزالة الأبيض من نهاية النص أو الأحرف المحددة مسبقًا

اللغة

trim(s
tring,charlist)
المعامل وصف
string مطلوب. يحدد النصوص التي يجب فحصها.
charlist

Optional. Specifies which characters to remove from the string. If omitted, the following characters are removed:

  • "\0" - NULL
  • "\t" - Tab
  • "\n" - New Line
  • "\x0B" - Vertical Tab
  • "\r" - Carriage Return
  • " " - Space

Technical Details

Return value: Returns the modified string
PHP Version: 4+
Update Log: In PHP 4.1, a new feature was added charlist Parameter.

More Examples

Example 1

Remove spaces from both sides of the string:

<?php
$str = " Hello World! ";
echo "Do not use trim: " . $str;
echo "<br>";
echo "Use trim: " . trim($str);
?>

The HTML output of the above code is as follows (please view the source code):

<!DOCTYPE html>
<html>
<body>
Do not use trim:  Hello World! <br>Use trim: Hello World!
</body>
</html>

The browser output of the above code is as follows:

Do not use trim: Hello World!
Use trim: Hello World!

Run Instances

Example 2

Remove newline characters (\n) from both sides of the string:

<?php
$str = "\n\n\nHello World!\n\n\n";
echo "Do not use trim: " . $str;
echo "<br>";
echo "Use trim: " . trim($str);
?>

The HTML output of the above code is as follows (please view the source code):

<!DOCTYPE html>
<html>
<body>
Do not use trim:
Hello World!
<br>Use trim: Hello World!
</body>
</html>

The browser output of the above code is as follows:

Do not use trim: Hello World!
Use trim: Hello World!

Run Instances