PHP/String/String Trim Chop — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:07, 26 мая 2010

Specify the characters in charlist by giving their hexadecimal value

<?
$str = "   string \t \n";
$str = trim ($str, "\0x09\0x0a");
echo( $str );
echo(strlen($str));
?>



String trim chop and length

<?
$original = "  AAAAAAA       ";
$chopped = chop($original);
$ltrimmed = ltrim($original);
$trimmed = trim($original);
print("The original is "$original"<BR>");
print("Its length is " . strlen($original) . "<BR>");
print("The chopped version is "$chopped"<BR>");
print("Its length is " . strlen($chopped) . "<BR>");
print("The ltrimmed version is "$ltrimmed"<BR>");
print("Its length is " . strlen($ltrimmed) . "<BR>");
print("The trimmed version is "$ltrimmed"<BR>");
print("Its length is " . strlen($trimmed) . "<BR>");
?>



Trimming Strings for "tab" and "return"

<html>
<head>
<title>Trim Functions</title>
</head>
<body>
<span>
<?php
     $TrailingSpaces = "Trailing Spaces             ";
     $Leadingspaces = "             Leading spaces";
     $TrailingAndLeadingSpaces = "            Trailing and Leading Spaces         ";
     $TabString = $HTTP_USER_AGENT . "  \n\n\n      \t\t \r\r";
   
     print("\"$TrailingSpaces\"" . "<br />");
     print("\"$Leadingspaces\"" . "<br />");
     print("\"$TrailingAndLeadingSpaces\"" . "<br />");
     print("\"$TabString\"" . "<br />");
   
     $TrailingSpaces = chop($TrailingSpaces);
     $Leadingspaces = ltrim($Leadingspaces);
     $TrailingAndLeadingSpaces = trim($TrailingAndLeadingSpaces);
     $TabString = trim($TabString);
   
     print("<br/>");
   
     print("\"$TrailingSpaces\"" . "<br />");
     print("\"$Leadingspaces\"" . "<br />");
     print("\"$TrailingAndLeadingSpaces\"" . "<br />");
     print("\"$TabString\"" . "<br />");
?>
</span>
</body>
</html>



Trimming Strings: ltrim

<html>
<head>
<title>Trim Functions</title>
</head>
<body>
<span>
<?php
     $TrailingSpaces = "Trailing Spaces             ";
     $Leadingspaces = "             Leading spaces";
     $TrailingAndLeadingSpaces = "            Trailing and Leading Spaces         ";
     $TabString = $HTTP_USER_AGENT . "  \n\n\n      \t\t \r\r";
   
     print("\"$TrailingSpaces\"" . "<br />");
     print("\"$Leadingspaces\"" . "<br />");
     print("\"$TrailingAndLeadingSpaces\"" . "<br />");
     print("\"$TabString\"" . "<br />");
   
     $TrailingSpaces = chop($TrailingSpaces);
     $Leadingspaces = ltrim($Leadingspaces);
     $TrailingAndLeadingSpaces = trim($TrailingAndLeadingSpaces);
     $TabString = trim($TabString);
   
     print("<br/>");
   
     print("\"$TrailingSpaces\"" . "<br />");
     print("\"$Leadingspaces\"" . "<br />");
     print("\"$TrailingAndLeadingSpaces\"" . "<br />");
     print("\"$TabString\"" . "<br />");
?>
</span>
</body>
</html>



Trimming Strings: trim

<html>
<head>
<title>Trim Functions</title>
</head>
<body>
<span>
<?php
     $TrailingSpaces = "Trailing Spaces             ";
     $Leadingspaces = "             Leading spaces";
     $TrailingAndLeadingSpaces = "            Trailing and Leading Spaces         ";
     $TabString = $HTTP_USER_AGENT . "  \n\n\n      \t\t \r\r";
   
     print("\"$TrailingSpaces\"" . "<br />");
     print("\"$Leadingspaces\"" . "<br />");
     print("\"$TrailingAndLeadingSpaces\"" . "<br />");
     print("\"$TabString\"" . "<br />");
   
     $TrailingSpaces = chop($TrailingSpaces);
     $Leadingspaces = ltrim($Leadingspaces);
     $TrailingAndLeadingSpaces = trim($TrailingAndLeadingSpaces);
     $TabString = trim($TabString);
   
     print("<br/>");
   
     print("\"$TrailingSpaces\"" . "<br />");
     print("\"$Leadingspaces\"" . "<br />");
     print("\"$TrailingAndLeadingSpaces\"" . "<br />");
     print("\"$TabString\"" . "<br />");
?>
</span>
</body>
</html>



Trim only tab and newline characters from $str

<?
$str = "   string \t \n";
$str = trim ($str, "\t\n");
echo( $str );
echo(strlen($str));
?>