Module4 Strings
Module4 Strings
PHP PROGRAMMING
Module 4: Strings
Working with Strings
Santa Monica College
Parse Stings
Computer Science &
Compare Stings Information Systems Dept.
What is a String?
A string is any sequence of characters, like "Hello world!", ‘a’, “123”, “a1b2c3”, $singleVar, etc. A string
can either be a text string surrounded by either single or double quotes. A variable with a string assigned
to it can also be considered a string, because it stores a string.
When working with strings, it is important to remember all strings must be enclosed in a single or
double quote and the ending quote and must match the beginning quote. If for example you want to the
include a single or double quote in the string, the opposite quotes should be used to declared the string.
For example $myQuote = ‘<p>”Never on Tuesday … “<p>’;
String Operators
There are two string operators in PHP. The first string operator is the concatenation operator ('.') a
single period, which returns the concatenation of its right and left arguments. The second is the
concatenating assignment operator ('.=') is the period and equal symbol, which appends the string on
the right side to the string on the left side. The string in these examples can be a variable with a string
value or the actual string.
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>
Escape Characters
When working with special characters in PHP strings, special care has to be taken to ensure the string is
interpreted by the PHP engine correctly. If a special character is added to a strings, unusual behavior or
fetal execution errors might occur.
For example, this line of code will have unexpected behavior. The single quote in the word “it’s” will
prematurely end the strings, when the PHP interpreter match the beginning single quote to the “it’s”
word single quote. Upon execution of this script, a parse error will occur.
By using escape characters in your string, it flags the PHP interpreter that the character that follows the
escape character serves a special purpose. In PHP the escape character is the backslash (\). By using the
backslash in front of a special character such a single quote (‘) or dollar sign ($) symbol, is telling the PHP
interpreter to treat the single quote or dollar symbol as normal characters and not as the beginning or
ending of a PHP string or in the case of the dollar symbol ($), not a variable but only as the text symbol
of a dollar sign.
This is only one example of where the escape character was use to include a special character in a string,
but the escape character can also perform an escape sequence where a string related change will be
made. For example \t will add a tab space to a string, \n will add a line break to a string. Here is a list of
escape sequences:
Sample Code:
String Syntax
Single quoted will present strings nearly identically as written. Meaning an escape sequence is not
always required. For example, variables and most escape sequences will not be interpreted. The
exception to this rule, is the escape sequence is required when single quotes used to enclosed a literal
string and the literal string has a single quote in it.
Double quote strings requires more use of escaped characters. All escape characters will be evaluated in
a string enclosed in double quotes. Variables in the strings will be evaluated. For example, the variable
$type in this statement
Sample Code:
$book = "Green Eggs and Ham";
echo "<p>Do you have any $book?</p>";
//Output: <p>Do you have any Green Eggs and Ham?</p>
The complex string syntax is a string that uses the curly brackets { } for use of complex expressions,
within the string.
<?php
$aVar = 'astring';
Count Characters
First knowing the length of a strings is often required before performing any other functions on the
string. For example, testing user input length for an account username or check correct number of
integers entered for a Social Security Number or credit card number. There are two built in functions
that will return the number of characters in a string, strlen() and str_word_count(). The function strlen()
will return the number of characters in a string. The function str_word_count() will return the number
words in the string.
<?php
//strlen() - count characters in a string
echo strlen("Hello"); //Output 5
$str = ' ab cd ';
echo strlen($str); //Output 7
?>
<?php
//str_word_count() – count words in a string
$str = "Hello fri3nd, you're
looking good today!";
Numeric Strings
Numbers can be stored in variable as strings. There are special cases where a number should be stored
as a string. For examples, social security numbers or a mailing addresses. Number sequences can be
stored as strings by surrounding the number with either single or double quotation marks. To test if a
string only contain numbers, the built in PHP function is_numeric() can be used.
<?php
$tests = array(
"42",
1337,
1337e0,
"not numeric",
array(),
9.1,
null
);
Output:
'42' is numeric
1337 is numeric
1337 is numeric
'not numeric' is NOT numeric
array (
) is NOT numeric
9.0999999999999996 is numeric
NULL is NOT numeric
?>
Sentences
When you are working with strings that are long sentences of a spoken language, it is common to
capitalize the first letter in the sentence. PHP has a built in function that will perform that exact task.
The function ucfirst(), will make the first letter in the string to uppercase.
<?php
echo ucfirst("hello world!"); //Output: Hello world!
?>
& &
" "
' '
< <
> >
Trim Strings
To remove any white spaces either at the beginner of ending of a string use the PHP trim() function. The
trim() function can also remove any predefined characters from the front or end of a string by passing a
char_list argument.
?>
Return Substrings
To return only part of a string, use the function substr(). The substr() function used to cut a part of a
string from a string, starting at a specified position.
$string1="Welcome to w3resource.com";
echo $string1;
echo '<br>';
echo substr($string1,1);
echo '<br>';
echo substr($string1,1,5);
echo '<br>';
echo substr($string1,0,10);
echo '<br>';
echo substr($string1,-1,1);
echo '<br>';
?>
Output:
Welcome to w3resource.com
elcome to w3resource.com
elcom
Welcome to
m
Shuffle String
The str_shuffle() function randomly shuffles all the characters of a string.
Syntax: str_shuffle(string)
<?php
echo str_shuffle("Hello World");
?>
//Output: Wdo llHreol
Syntax: strrev(string)
<?php
$main_string='www.awebsite.com';
echo strrev($main_string);
?>
//Output: moc.etisbewa.www
Multiple Strings
PHP supports functions for working with a string into substrings, merging strings, and modifying string
due to a match with another. In this section of the reading, we will study how to work with multiple
stings.
Locating Substrings
The strpos() function finds the position of the first occurrence of a substring in a string. It is a case
sensitive search of the substring. The function will return the position of the first occurrence the
substring within the string. The function strops() takes two arguments. The first strpos() function
argument is the string you want to search, and the second strpos() function argument contains the
substring to search for. If the substring is not found, the strpos() function returns a Boolean value of
FALSE.
<?php
$mystring = 'abcdeghijk';
$findme = 'e';
$pos = strpos($mystring, $findme);
Similar Functions:
strchr() : The strchr() function searches for the first occurrence of a string inside another string.
strrchr() : The strrchr() function finds the position of the last occurrence of a string within another string,
and returns all characters from this position to the end of the string.
<?php
echo strrchr("Hello world! What a beautiful day!",What);
?>
//Output: What a beautiful day!
str_replace() : The str_replace() function replaces some characters with some other characters in a
string.
<!DOCTYPE html>
<html>
<body>
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "<br>" . "Replacements: $i";
?>
</body>
</html>
Empty String
The PHP empty() function check if a variable is empty or not. It is sometimes necessary to know if a
variable empty before performing certain tasks using the variable. For example, an online form that gets
processed in PHP can be submitted by the user with empty fields on the form. When the form fields are
assigned to variables, it is possible some of the variables will be empty. In these situations, empty
function can be extremely useful.
Syntax: empty(var_name)
Returns true if var_name has an empty and zero value.
To test if a variable is not empty use the NOT (!) operator with the empty() function. The not operator
can be used to change FALSE to TRUE and TRUE to FALSE. For example, !(FALSE) is TRUE. The statement
!(empty(var_name)) will return TRUE when a variable is not empty. The alternative is to use the isset()
function. The isset () function is used to check whether a variable is set or not. The isset() function return
false if testing variable contains a NULL value.
Syntax: isset(variable)
Return TRUE if the variable exists and has a value not equal to NULL. FALSE otherwise.
$var = 0;
To assign the next token to $variable, call the strtok() function again, but only pass to it a single
argument containing the separator. PHP engine will keep track of the current token and assigns the next
token to $variable, starting at the first character after the separator, each time the strtok() function is
called and until the end of the string is reached.
<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, " \n\t");
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
The explode() function breaks a string into an array. Returns an array of strings, each of which is a
substring of string formed by splitting it on boundaries formed by the string delimiter. The explode
function is required and it specific the string delimiter, the sting is the string to be split and limit is an
optional argument that specific the number array elements to return.
//Output
Array (
[3] => a
[4] => beautiful
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) !== 0) {
echo '$var1 is not equal to $var2 in a case sensitive string
comparison';
}
else {
echo '$var1 is equal to $var2 in a non-case sensitive string
comparison';
}
//Output: $var1 is equal to $var2 in a non-case sensitive string
comparison
?>
Regular Expressions
A regular expression is a method of representing a string matching pattern. Regular expressions enable
strings that match a particular pattern within textual data records to be located and modified and they
are often used within utility programs and programming languages that manipulate textual data.
Regular expressions are extremely powerful. PHP developers commonly use regular expression to
validate user input. Such input can include credit card numbers, phone numbers, zip code, etc.
PHP supports Perl Compatible Regular Expressions (PCRE). Some of the PCRE functions include
preg_match(), preg_match_al(), preg_replace(), preg_split,
The function preg_match() is the most commonly used regular expression function. The function takes a
regular expression patter as an argument and then the string to be searched. The function will return a 1
if a match was found and 0 if no match was made. Notice how this code is case insensitive.
$String = "welcome to los angeles";
if (preg_match("/Los Angeles/", $String))
echo "<p>Match found</p>";
else
echo "<p>No match</p>";
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can
be any non-alphanumeric, non-backslash, non-whitespace character. Commonly used delimiters are
forward slashes (/), hash signs (#) and tildes (~).
Examples:
/abc def/
#^[^0-9]$#
+aword+
%[a-z*]%
Bracket style delimiters are also possible, where the opening and closing brackets are the starting and
ending delimiter, respectively. (), {}, [] and <> are all valid bracket style delimiter pairs. Example: (here
[is] a (regexp))
When the delimiter is part of the string that needs to be match, use the backslash to escape the
delimiter special meaning.
/www:\/\//
#ftp://
Meta-characters ¶
The usefulness of regular expressions is its ability to include different and repetitions in the pattern
matching. By using meta-characters, a more advance regular expression pattern can be encoded. Meta-
characters are interpreted in some special way depending on the text directly in front of or in back of
the meta character.
There are two different types of meta-characters. The first type is matches anywhere in the pattern,
except in the square brackets. The second type are recognized in square brackets.
Matching Any Character
Use the Meta Character period ( . ) to match any single character. For example, a developer has a string
and needs to know if it is 5 characters long. Because the variable only contains 4 characters, the
preg_match() function returns a value of 0.
$ZIPCODE = "915";
This example has a variable with five characters. The preg_match will match the regular expression /…../
with the variable with five characters. One characters per period is matched.
$ ZIPCODE = "90045";
To take it a step further and required the $ZIPCODE variable to start with a 9 for California zip codes,
replace the first period in the regular express with a 9. It is now requiring that the variable $ZIPCODE
start with a 9 for the regular expression to match and return 1.
$ ZIPCODE = "90045";
This example specifies that the $URL variable starts with http. The regular express /http/ requires that
http appear anywhere in $URL but by adding the ^ to the regular expression /^http/, it now requires
that http appear the beginning the string.
$URL = "http://www.awebsite.com";
$result = preg_match("/^http/", $URL);
print_r($result); // returns 1
By using the dollar symbol ( $ ), a regular expression must match the last characters in the string. c$
matches c in abc, while a$ does not match because a is not the last character.
Here is an example where the code requires that the string ends with .gov domain suffix. This code will
return a value of 1, because $URL does end with a .gov domain.
$URL = "http://www.irs.gov";
$result = preg_match("/.gov$/", $URL);
print_r($result); // returns 1
Matching Metacharacters
When there is a need to match one of the characters that function as a regular expression meta
character, use the escape character ( \ ) to flag the PHP interpreter to not treat the character that
follows as a meta character but as a normal character. For example, if a regular expression need to
check for a period to appear in a URL, then the period must first be escaped. /\.gov/
$URL = "http://www.irs-gov";
$URL = "http://www.irs-gov";
// returns 0 because now \. must represent a .
echo preg_match("/\.gov$/", $URL);
To print the dollar symbol in a string in PHP is slightly tricky. The $ symbol represent a variable in PHP.
To escape the dollar symbol, there are two options. The first is to used double around the strings and
then use three backslashes to (\\\$) in the string. The second method is to use single quotes around the
string and use only one escape backslash (\$).
$dollarAmt="$123.45";
Repetition
Repetition is specified by quantifiers, which can follow any of the following items:
The general repetition quantifier specifies a minimum and maximum number of permitted matches, by
giving the two numbers in curly brackets (braces), separated by a comma.
There is also the use of a question mark quantifier that represents an optional pattern directly in front of
the question mark. For example, in the below example the s in https is an optional character. Both http
and https will match with the use of this regular express.
$URL = "http://www.whitehouse.gov";
The addition quantifier (+) specifies that one or more sequential occurrences of the preceding
characters match, whereas the asterisk quantifier (*) specifies that zero or more sequential occurrences
of the preceding characters match. As a simple example, the following code demonstrates how to
ensure that data has been entered in a required field.
$Name = "John";
Similarly, because a numeric string might contain leading zeroes, the following code demonstrates how
to check whether the $NumberString variable contains zero or more leading zeroes:
$NumberString = "04544";
The { } quantifiers allow you to more precisely specify the number of times that a character must repeat
sequentially. The following code shows a simple example of how to use the { } quantifiers to ensure that
a ZIP code consists of at least five characters:
The preceding code uses the period metacharacter and the { } quantifiers to ensure that the $ZIP
variable contains a minimum of five characters. The following code specifies that the $ZIP variable must
consist of at least five characters but a maximum of 10 characters, in case the ZIP code contains the dash
and four additional numbers that are found in a ZIP+4 number:
Specifying Subexpressions
As you learned earlier, regular expression patterns can include literal values; any strings you validate
against a regular expression must contain exact matches for the literal values contained in the pattern.
You can also use parentheses metacharacters (( and )) to specify the characters required in a pattern
match. Characters contained in a set of parentheses within a regular expression are referred to as a
subexpression or subpattern. Subexpressions allow you to determine the format and quantities of the
enclosed characters as a group. As an example, consider the following pattern, which defines a regular
expression for a telephone number: "/^(1 )?(\(.{3}\) )?(.{3})(\-.{4})$/"
The first and second groups in the preceding pattern include the ? quantifier. This allows a string to
optionally include a 1 and the area code. If the string does include these groups, they must be in the
exact format of “1 ” for the first pattern and “(nnn) ” for the second pattern, including the space
following the area code. Similarly, the telephone number itself includes two groups that require the
number to be in the format of “nnn” and “–nnnn.” Because the “1 ” and the area code pattern are
optional, all of the following statements return a value of 1: