PHP/String/trim

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

Checking the length of a trimmed string

   <source lang="html4strict">

<?

   $zipcode = trim("12344");
   $zip_length = strlen($zipcode);
   if ($zip_length != 5) {
       print "Please enter a ZIP code that is 5 characters long.";
   }

?>

 </source>
   
  


Combining trim() and strlen()

   <source lang="html4strict">

if (strlen(trim($_POST["name"])) == 0) {

   $errors[] = "Your name is required.";

}

 </source>
   
  


string trim ( string str [, string trim_chars] ) strips spaces, new lines, and tabs

   <source lang="html4strict">

<?

   $a = trim(" testing ");
   $b = trim(" testing ", " teng");

?>

 </source>
   
  


trim() function removes all whitespace from both sides of string

   <source lang="html4strict">

Its syntax is: string trim (string string) It will also remove the special characters "\n", "\r", "\t", "\v" and "\0". <? print ">".trim(" asdf ")."<"; ?>

 </source>