PHP Practice Exercise: String Basics
String: a sequence of zero or more characters, enclosed within single or double
quotation marks.
General Form:
$stringName [ = "string value" ];
Examples: firstName = " LAURA\t"; lastName = 'THOMPSON\n';
String operations: The common operators (<, >, ==) may be used with strings
as well as concatenation (.), and functions for string manipulation including
trimming, changing case, finding length, the position of a substring and
extracting a substring.
1. The string length function -- strlen(strArg)
this function returns the number of characters currently in a string argument.
Find the length of firstName by invoking strlen().
2. Trimming strings -- trim(strArg[,listOfChars])
To strip whitespace from the start and end of a string and return the
resulting string. Related functions are ltrim() and rtrim().
Apply trim() to firstName.
3. Changing case -- strtolower(), strtoupper(), ucfirst()
Set names to lower case and capitalize the first letter of firstName and
lastName.
4. Concatenation (.) where a dot indicates to join two strings:
Concatenate firstName to lastName and assign the result to fullName.
5. Concatenate lastName, a comma(,), a space, and firstName with the result
assigned to editedName.
6. strpos(sourceString,substring[,index])
To find the position of a substring within a source string with an optional
index to specify where to begin the search. This function searches the
string for the first occurrence of a particular substring.
Use strpos to analyze editedName for the position of the first comma (,).
7. The substring function -- substr(strArg, start[, length]) where strArg
is the source string, start an integer specifying the starting position,
and optionally the length of the substring.
Extract the lastName from the string variable editedName.
Extract the firstName (assume it begins two positions after the comma).
8. The explode function -- explode($sep,$str)
To return an array of strings separated by the string value in $sep.
[See also, implode($sep, $stringArray) ]
Explode editedName into an array with two elements.