Tedd's Functions Revisited:
Demonstrates three custom String Functions
Original string is: Hello My WorldRight 4 characters are: orld
Left 5 characters are: Hello
Middle 6 characters taken 3 characters from the left are: lo My
Demonstrates three custom String Functions
Original string is: Hello My World
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo("<h1>Tedd's Functions Revisited:</h1>");
echo("<p>Demonstrates three custom String Functions</p>");
// ===== main script ========
$str ='Hello My World'; // the string
echo("Original string is: $str");
echo('<br>');
$str2 = right($str,4); // pull the right 4 characters from the string
echo("Right 4 characters are: $str2");
echo('<br>');
$str2 = left($str, 5); // pull the left 5 characters from the string
echo("Left 5 characters are: $str2");
echo('<br>');
$str2 = mid($str, 3, 6);// pull 6 characters starting 3 characters from the left
echo("Middle 6 characters taken 3 characters from the left are: $str2");
echo('<br>');
// ============= functions ====================
// ------- right function -------
// This function returns the right-most number of characters from a string
// Example: $string = "123456789" : right($string, 3) returns "789"
function right($string, $length)
{
$str = substr($string, -$length, $length);
return $str;
}
// ------- left function -------
// This function returns the left-most number of characters from a string
// Example: $string = "123456789" : left($string, 3) returns "123"
function left($string, $length)
{
$str = substr($string, 0, $length);
return $str;
}
// ------- mid function -------
// This function returns the middle number of characters from a string
// starting after a set number of charcters from the left
// Example: $string = "123456789" : mid($string, 3, 5) returns "45678"
function mid($string, $left_start, $length)
{
$str = substr($string, $left_start, $length);
return $str;
}
include('includes/footer.php');
?>