Kaylyn's Functions Revisited:
Three Custom String Functions
Original string is: We are halfway there!Right 3 characters are: re!
Left 8 characters are: We are h
Middle 9 characters taken 2 characters from the left are: are half
Three Custom String Functions
Original string is: We are halfway there!
<?php
include('includes/header.php');
error_reporting(E_ALL);
echo("<h1>Kaylyn's Functions Revisited:</h1>");
echo("<p>Three Custom String Functions</p>");
// ===== main script ========
$str ='We are halfway there!'; // the string
echo("Original string is: $str");
echo('<br>');
$str2 = right($str,3); // pull the right 4 characters from the string
echo("Right 3 characters are: $str2");
echo('<br>');
$str2 = left($str, 8); // pull the left 5 characters from the string
echo("Left 8 characters are: $str2");
echo('<br>');
$str2 = mid($str, 2, 9);// pull 6 characters starting 3 characters from the left
echo("Middle 9 characters taken 2 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');
?>