Using the string functions

Functions that modify strings

ltrim|rtrim|trim($str)
str_pad($str, $len [, $pad[, $type]])
lcfirst|ucfirst($str)
ucwords($str)
strtolower|strtoupper($str)
strrev($str)
str_shuffle($str)
str_repeat($str, $i)
str_replace|str_ireplace($str1, $new, $str2)

Code that trims and pads a string

<?php
  $name = '   ray harris   ';
  $name = ltrim($name);                          // $name is 'ray harris   '
  $name = rtrim($name);                          // $name is 'ray harris'
  $name = str_pad($name, 13);                    // $name is 'ray harris   '
  $name = str_pad($name, 16, ' ', STR_PAD_LEFT); // $name is '   ray harris   '
  $name = trim($name);                           // $name is 'ray harris'
?>

Code that works with capitalization

<?php
  $name = ucfirst($name);       // $name is 'Ray harris'
  $name = lcfirst($name);       // $name is 'ray harris'
  $name = ucwords($name);       // $name is 'Ray Harris'
  $name = strtolower($name);    // $name is 'ray harris'
  $name = strtoupper($name);    // $name is 'RAY HARRIS'
?>

Back