Using the string functions

Code that changes the sequence of the characters

<?php
  $name = "RAY HARRIS";
  $name = strrev($name);        // $name is 'SIRRAH YAR'
  $name = str_shuffle($name);   // $name is 'SHYIRRR AA'
                                // (for example)
?>

Code that repeats a string

<?php  
  $sep = str_repeat('*', 10);  // $sep is '**********'
?>

Code that replaces periods with dashes

<?php  
  $phone = '554.555.6624';
  $phone = str_replace('.', '-', $phone); // $phone is '554-555-6624'
?>

Code that replaces one string with another

<?php  
  $message = 'Hello Ray';
  $message = str_ireplace('hello', 'Hi', $message); // $message is 'Hi Ray'
?>

Back