Download - Strings and Arrays
![Page 1: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/1.jpg)
Strings and Arrays
![Page 2: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/2.jpg)
String
Is a sequence of characters. Example: “hello” , “how are
you?” , “123”,and “!@#$%” are all valid string values.
![Page 3: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/3.jpg)
Creating and Accessing a String
$myString = ‘hello‘; $myString = “hello”;
<?php $myString='hello there!';
echo "Hello $myString";
echo 'Hello $myString';
?>
![Page 4: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/4.jpg)
{ }
use the curly brackets to distinguish the variable name from the rest of the string
$myFavorite=”cat”;echo “My favorite animals are
{$myFavorite}s”;
![Page 5: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/5.jpg)
Accessing Characters within a String$myString=”hello world”;echo $myString[0]; // displays h
![Page 6: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/6.jpg)
String Functions
strlen() - Calculating length of array<?php $name = “Simon Stobart”; $stringlength = strlen($name); echo $stringlength;?>
![Page 7: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/7.jpg)
String Functions
str_word_count() - returns the number of words in a string
echo str_word_count(“Hello There”); // returns 2
![Page 8: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/8.jpg)
String FunctionsSubstr() - extract sequence of
characters in a stringThe string to extract the characters fromThe position to start extracting the characters. If
you use a negative number, substr() counts backward from the end of the string
The number of characters to extract. If you use a negative number, substr() misses that many characters from the end of the string instead. This parameter is optional; if left out, substr()extracts
from the start position to the end of the string
![Page 9: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/9.jpg)
$myString = “Hello, world!”;
echo substr( $myString, 0, 5 ) . “ < br/ > ”; // Displays ‘Hello’
echo substr( $myString, 7 ) . “ < br/ > ”; // Displays ‘world!’
echo substr( $myString, -1 ) . “ < br/ > ”; // Displays ‘!’
echo substr( $myString, -5, -1 ) . “ < br/ > ”; // Displays ‘orld’
![Page 10: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/10.jpg)
String Functions
strpos() - find out exactly where a string of text occurs within another string
$myString = “Hello, world!”;
echo strpos( $myString, “wor” ); // Displays ‘7’
echo strpos( $myString, “xyz” ); // Displays ‘’ (false)
![Page 11: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/11.jpg)
strrpos() - finds the last match in the string, rather than the first
$myString = “Hello, world!”;
echo strpos( $myString, “o” ) . “ < br / > ”; // Displays ‘4’
echo strrpos( $myString, “o” ) . “ < br / > ”; // Displays ‘8’
![Page 12: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/12.jpg)
String Functions
substr_count() - returns the number of times the text was found in the string
$myString = “I say, nay, nay, and thrice nay!”;
echo substr_count( $myString, “nay” ); // Displays ‘3’
![Page 13: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/13.jpg)
String Functions
strpbrk() - returns the
portion of the string from the first matched character to the end of the string. If none of the characters in
the set are found in the string, strpbrk() returns false .
![Page 14: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/14.jpg)
$myString = “Hello, world!”;
echo strpbrk( $myString, “abcdef” ); // Displays ‘ello, world!’
echo strpbrk( $myString, “xyz” ); // Displays ‘’ (false)
$username = “[email protected]”;
if ( strpbrk( $username, “@!” ) ) echo “@ and ! are not allowed in usernames”;
![Page 15: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/15.jpg)
String Functions
strstr(searchstring, lookingstring) find a specific part of a string inside
another string, returns the remainder of the input string
<?php $name = “Simon Stowart”; $strOutput =strstr($name,”St”);
echo “The result is”.$strOutput;?>
![Page 16: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/16.jpg)
String Functions
str_replace(lookingforstring,replatestring,searchstring)Replace parts of the string with
another string
![Page 17: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/17.jpg)
<?php
$strSentence="The use of italics can be useful to highlight certain words. ";
$strOutput = str_replace("italics","<i>italics</i>",$strSentence);
echo $strSentence ."<br>";
echo "The output now looks like: ".$strOutput;?>
?>
![Page 18: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/18.jpg)
String Functions
substr_replace() replaces a specified portion of the target string with another string
$myString = “It was the best of times, it was the worst of times,”;
// Displays “It was the bananas”
echo substr_replace( $myString, “bananas”, 11 ) . “ < br/ > ”;
![Page 19: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/19.jpg)
String Functions
strrev(string)Returns the string in reverse order<?php $name = “Simon Stowart”; $strOutput = strrev($name); echo $name. “backwards is ”.
$strOutput;?>
![Page 20: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/20.jpg)
String Functions
strtoupper(), strtolower(),ucfirst(),ucwords();Changes the case of string
![Page 21: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/21.jpg)
<?php
echo strtoupper("simon stowart")."<br>".strtolower("SIMON SOTWART");
echo "<br>".ucfirst("simon stowart")."<br>".ucwords("simon stowart");?>
?>
![Page 22: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/22.jpg)
Formatting Functions
printf() trim() removes white space from
the beginning and end of a stringltrim() removes white space only
from the beginning of a stringrtrim() removes white space only
from the end of a string
![Page 23: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/23.jpg)
Array
SyntaxtheArray =
array(array1,array2… ,arrayN);$arrColors =
array(“Red”,”Blue”,”Yellow”,”White”);
![Page 24: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/24.jpg)
<?php$arrColors =
array(“Red”,”Blue”,”Yellow”,”White”);
for ($ctr = 0; $ctr < 5; $ctr++)
echo “<p>”.$arrColors[$ctr].”<p>”;
?>
![Page 25: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/25.jpg)
Array
Accessing array elementsIndex or key
![Page 26: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/26.jpg)
<?php
$arrColors = array(0=>"Red",1=>"Blue",3=>"Yellow",5=>"White");
for ($ctr = 0; $ctr < 6; $ctr++)
echo "<p>".$arrColors[$ctr]."<p>";
?>
![Page 27: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/27.jpg)
foreach(array as value) statement<?php
$arrColors = array(0=>"Red",1=>"Blue",3=>"Yellow",5=>"White");$ctr =0;foreach ($arrColors as $strColor){
echo "<p>".$ctr ." " .$strColor."<p>"; $ctr++; }?>
![Page 28: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/28.jpg)
Arrays
Nonmerical keys<?php
$arrColors = array("red"=>"Red","blue"=>"Blue","yello"=>"Yellow","white"=>"White");
foreach ($arrColors as $strkey => $strColor)
echo "<p>".$strkey ." : " .$strColor."<p>";
?>
![Page 29: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/29.jpg)
Multi-dimensional array<?php
$arrCars = array (array("Fords","Mazda","Toyota"),
array("Blue","Red","Green"),
array(1,2,4));
for ($ctr = 0; $ctr <3; $ctr++){
$makar = $arrCars[0][$ctr];
$color = $arrCars[1][$ctr];
$qty = $arrCars[2][$ctr];
echo "<p> Maka: ".$maker." Color: ".$color. " Quantity: ".$qty ."</p>";
}
?>
![Page 30: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/30.jpg)
Counting array elements
$size = count(array)
![Page 31: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/31.jpg)
Creating Stack
array_pop(array)array_push(array,element)
![Page 32: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/32.jpg)
Example
<?php$arrColor = array("Red","Blue");print_r($arrColor);
$strColor = array_pop($arrColor);echo "<br> Deleted color: ".$strColor."<br>";print_r($arrColor);array_push($arrColor,"Green");echo "<br>";print_r($arrColor);
?>
![Page 33: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/33.jpg)
Explode
Array = explode(separator, string);the first string indicating the what
characters will be use to search the string to separate it into individual array elements
The second parameter is the string itself
![Page 34: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/34.jpg)
<?php$pizza = "piece1 piece2 piece3 piece4 piece5
piece6";$pieces = explode(" ", $pizza);print_r($pieces);
?>
![Page 35: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/35.jpg)
Implode
String = implode(separator, array);
The first string indicating what characters it will use to separate each element and the second is an array
![Page 36: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/36.jpg)
<?php
$array = array('lastname', 'email', 'phone');$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
?>
![Page 37: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/37.jpg)
Using arrays in forms
<?php$arrNames = array("Simon","Liz","Gemma","Hayley");$strNames = implode("|",$arrNames);
?>
<html><body><form method = "post" action="display.php"><input type = "hidden" name="strNames" value =" <?php echo $strNames; ?>"><br><br><input type = "text" name="names" maxlength="50" ><input type = "submit" name="submit" value="click"></form></body></html>
![Page 38: Strings and Arrays](https://reader035.vdocuments.mx/reader035/viewer/2022070411/568146f2550346895db42660/html5/thumbnails/38.jpg)
<?phpif(isset($_POST['submit'])){
echo "<p> The array contains: </p>";$strNames = $_POST['strNames']."|".$_POST['names'];$arrNames = explode("|",$strNames);foreach($arrNames as $key => $value)
echo "<p>[". $key ."]".$value ."</p>";}
?>