Kaity's Functions Revisited:
Demonstrates three custom String Functions
Original string is: Never Give UpRight 8 characters are: Give Up
Left 3 characters are: Nev
Middle 4 characters taken 6 characters from the left are: Give
Demonstrates three custom String Functions
Original string is: Never Give Up
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo("<h1>Kaity's Functions Revisited:</h1>");
echo("<p>Demonstrates three custom String Functions</p>");
// ===== main script ========
$str ='Never Give Up'; // the string
echo("Original string is: $str");
echo('<br>');
$str2 = right($str,8); // pull the right 4 characters from the string
echo("Right 8 characters are: $str2");
echo('<br>');
$str2 = left($str, 3); // pull the left 5 characters from the string
echo("Left 3 characters are: $str2");
echo('<br>');
$str2 = mid($str, 6, 4);// pull 6 characters starting 3 characters from the left
echo("Middle 4 characters taken 6 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');
?>