Strings Revisited

Strings Revisited

The string is: Now is the time for all good men.

The length of the string is: 35

Character (ASCII[32]) -- -- is in position 0
Character (ASCII[78]) --N-- is in position 1
Character (ASCII[111]) --o-- is in position 2
Character (ASCII[119]) --w-- is in position 3
Character (ASCII[32]) -- -- is in position 4
Character (ASCII[105]) --i-- is in position 5
Character (ASCII[115]) --s-- is in position 6
Character (ASCII[32]) -- -- is in position 7
Character (ASCII[116]) --t-- is in position 8
Character (ASCII[104]) --h-- is in position 9
Character (ASCII[101]) --e-- is in position 10
Character (ASCII[32]) -- -- is in position 11
Character (ASCII[116]) --t-- is in position 12
Character (ASCII[105]) --i-- is in position 13
Character (ASCII[109]) --m-- is in position 14
Character (ASCII[101]) --e-- is in position 15
Character (ASCII[32]) -- -- is in position 16
Character (ASCII[102]) --f-- is in position 17
Character (ASCII[111]) --o-- is in position 18
Character (ASCII[114]) --r-- is in position 19
Character (ASCII[32]) -- -- is in position 20
Character (ASCII[97]) --a-- is in position 21
Character (ASCII[108]) --l-- is in position 22
Character (ASCII[108]) --l-- is in position 23
Character (ASCII[32]) -- -- is in position 24
Character (ASCII[103]) --g-- is in position 25
Character (ASCII[111]) --o-- is in position 26
Character (ASCII[111]) --o-- is in position 27
Character (ASCII[100]) --d-- is in position 28
Character (ASCII[32]) -- -- is in position 29
Character (ASCII[109]) --m-- is in position 30
Character (ASCII[101]) --e-- is in position 31
Character (ASCII[110]) --n-- is in position 32
Character (ASCII[46]) --.-- is in position 33
Character (ASCII[32]) -- -- is in position 34

The first occurrence of 'l' is in position 22

The first occurrence of 'd' is in position 28


The string located at the second index of the array is: Now is the time for all good men.
The third character of the Now is the time for all good men. string at the second index of the array is: w

Explode the string Now is the time for all good men. into an array
Array
(
    [0] => 
    [1] => Now
    [2] => is
    [3] => the
    [4] => time
    [5] => for
    [6] => all
    [7] => good
    [8] => men.
    [9] => 
)
<?php
include ('../header/assignmentHeader.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Strings Revisited</title>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
</head>
<body>

<h1>Strings Revisited</h1>

<?php
$string 
= " Now is the time for all good men. ";
echo 
"<p>The string is: " . $string . "</p>";
echo 
"<p>The length of the string is: " . strlen($string) . "</p>";
for (
$i = 0; $i < strlen($string); $i++) {
    
$character = substr($string, $i, 1);
    
$ascii = ord($character); //Convert the first byte of a string to a value between 0 and 255

    
echo "Character (ASCII[$ascii]) --$character-- is in position $i<br>";
}

echo 
"<br>";
echo 
"<p>The first occurrence of 'l' is in position " . strpos($string, "l") . "</p>";
echo 
"<p>The first occurrence of 'd' is in position " . strpos($string, "d") . "</p>";
echo 
"<br>";

$array = array();
$array[1] = trim($string);// removes trailing white space

echo "The string located at the second index of the array is: " . $array[1] . "<br>";
echo 
"The third character of the " . $array[1] . " string at the second index of the array is: " . substr($array[1], 2, 1);
echo 
"<br><br>";
echo 
"Explode the string " . $string . " into an array<br>";
$words = explode(" ", $string);

echo 
"<pre>";
print_r($words);
echo 
"</pre>";

?>
</body>
</html>
<?php
show_source
(__FILE__);
include(
'../footer/footer.php');
?>