0% found this document useful (0 votes)
52 views13 pages

Ephrem Tesfaye - IP-II - Chapter-Five

The document discusses string manipulation and regular expressions in PHP. It covers creating, accessing and modifying strings, using built-in functions like trim(), concatenate, change case, join and split strings. It also discusses validating user input with regular expressions.

Uploaded by

Enough
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views13 pages

Ephrem Tesfaye - IP-II - Chapter-Five

The document discusses string manipulation and regular expressions in PHP. It covers creating, accessing and modifying strings, using built-in functions like trim(), concatenate, change case, join and split strings. It also discusses validating user input with regular expressions.

Uploaded by

Enough
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Internet Programming II Chapter Five – Strings and Regular Expressions

College of Engineering, Technology and Computational Sciences


Department of Computer Science

Course Title: Internet Programming II (COSC 3032)

Topic: String Manipulation and Regular Expressions

Topic Contents

5. String Manipulation and Regular Expression


5.1 String manipulation in PHP
5.2 Regular Expression in PHP

Objectives
After studying this chapter, you should be able to:
 Understand the concept of String
 Create, access and manipulate string efficiently
 Implement String manipulation PHP built in functions in your program
 Accept and prepare user input for database entry
 Validate users input and also authenticate user
 Understand the concept of regular expression
 Use regular expression and implement it using built in PHP functions to check
users input

Compiled By: Ephrem Tesfaye Tsidu 1


Internet Programming II Chapter Five – Strings and Regular Expressions

5.1 String manipulation in PHP

Basic Concept

A string is a sequence of letters, numbers, special characters and arithmetic values or combination
of all. The simplest way to create a string is to enclose the string literal (i.e. string characters) in
single or double quotation marks (‘’, “”), like this

$ my_string = ‘Hello’;

However, single and double quotation marks work in different ways. Strings enclosed in single
quotes are treated almost literally, whereas the strings delimited by the double quotes replaces
variables with the string representations of their values as well as specially interpreting certain
escape sequence.

There are no artificial limits on string length within the bounds of available memory, you ought to
be able to make arbitrarily long strings

Some Common Operations and Built-in function in PHP strings

The Concatenation Operator

To concatenate two string variables together, use the dot (.) operator

<?php
$string1="Hello World";
$string2="1234";
echo $string1 ." ". $string2;
?>
This will produce the following result Hello World 1234

If we look at the code above you see that we used the concatenation operator two times. This is
because we had to insert a third string. Between the two string variables we added a string with a
single character, an empty space, to separate the two variables.

Compiled By: Ephrem Tesfaye Tsidu 2


Internet Programming II Chapter Five – Strings and Regular Expressions

Trimming Strings

The first step in tidying up is to trim any excess whitespace from the string. Although this step is
never compulsory, it can be useful if you are going to store the string in a file or database, or if
you’re going to compare it to other strings.

PHP provides three useful functions for this purpose. In the beginning of the script when you give
short names to the form input variables, you can use the trim() function to tidy up your input data
as follows:

$name = trim($_POST['name']);

The trim() function strips whitespace from the start and end of a string and returns the resulting
string. The characters it strips by default are newlines, horizontal and vertical tabs, end-of-string
characters and spaces. You can also pass it a second parameter containing a list of characters to
strip instead of this default list. Depending on your particular purpose, you might like to use the
ltrim() or rtrim() functions instead. They are both similar to trim(), taking the string in question as
a parameter and returning the formatted string. The difference between these three is that trim()
removes whitespace from the start and end of a string, ltrim() removes whitespace from the start
(or left) only, and rtrim() removes whitespace from the end (or right) only.

Formatting Strings for Output

PHP includes a set of functions that you can use to reformat a string in different ways for different
purposes.

So far, you have used the echo language construct to print strings to the browser. PHP also supports
a print() construct, which does the same thing as echo, but returns a value, which is always equal
to 1.

Both of these techniques print a string “as is.” You can apply some more sophisticated formatting
using the functions printf() and sprintf(). They work basically the same way, except that printf()
outputs a formatted string and sprintf() returns a formatted string.

Compiled By: Ephrem Tesfaye Tsidu 3


Internet Programming II Chapter Five – Strings and Regular Expressions

The first parameter passed to both of these functions is a format string that describes the basic
shape of the output with format codes instead of variables. The other parameters are variables that
will be substituted in to the format string.

For example, using echo, you can use the variables you want to print inline, like this:

echo "Total amount of order is $total.";

To get the same effect with printf(), you would use

printf ("Total amount of order is %s.", $total);

The %s in the format string is called a conversion specification. This one means “replace with a
string.” In this case, it is replaced with $total interpreted as a string. If the value stored in $total
was 12.4, both of these approaches would print it as 12.4.

The advantage of printf() is that you can use a more useful conversion specification to specify that
$total is actually a floating-point number and that it should have two decimal places after the
decimal point, as follows:

printf ("Total amount of order is %.2f", $total);

Given this formatting, and 12.4 stored in $total, this statement will print as 12.40.

You can have multiple conversion specifications in the format string. If you have n conversion
specifications, you will usually have n arguments after the format string. Each conversion
specification will be replaced by a reformatted argument in the order they are listed. For example,

printf ("Total amount of order is %.2f (with shipping %.2f) ", $total, $total_shipping);

Here, the first conversion specification uses the variable $total, and the second uses the variable
$total_shipping.

Conversion specification type codes

 % - A literal % character.
 b - Interpret as an integer and print as a binary number.
 C - Interpret as an integer and print as a character.
 d - Interpret as an integer and print as a decimal number.

Compiled By: Ephrem Tesfaye Tsidu 4


Internet Programming II Chapter Five – Strings and Regular Expressions

 e - Interpret as a double and print in scientific notation. Precision is the number of digits
after the decimal point.
 E - Same as e but a capital E will be printed.
 f - Interpret as a float and print as a locale-aware floating-point number.
 F - Interpret as a float and print as a non-locale-aware floating-point number.
 g - This is the shorter output, given a choice of e or f as the specification type.
 G - This is the shorter output, given a choice of E or F as the specification type.
 - Interpret as an integer and print as an octal number.
 s - Interpret as a string and print as a string.
 u - Interpret as an integer and print as an unsigned decimal.
 x - Interpret as an integer and print as a hexadecimal number with lowercase letters for the
digits a–f.
 X - Interpret as an integer and print

Changing the Case of a String

You can also reformat the case of a string. This capability is not particularly useful for the sample
application, but we’ll look at some brief examples.

 strtoupper() - Turns string to uppercase


 strtolower() - Turns string to lowercase
 ucfirst() - Capitalizes first letter from the first word in the string
 ucwords() Capitalizes first letter from each word in the string

Example

$subject = “Feedback from web site”;

strtoupper($subject); //FEEDBACK FROM WEB SITE

strtolower($subject); //feedback from web site

ucfirst($subject); //web site

ucwords($subject); //Web Site

Compiled By: Ephrem Tesfaye Tsidu 5


Internet Programming II Chapter Five – Strings and Regular Expressions

Joining and Splitting Strings with String Functions

Often, you may want to look at parts of a string individually. For example, you might want to look
at words in a sentence (say, for spellchecking) or split a domain name or email address into its
component parts. PHP provides several string functions that allow you to do this.

The first function you could use for this purpose, explode(), has the following prototype: array
explode(string separator, string input [, int limit]); This function takes a string input and splits it
into pieces on a specified separator string. The pieces are returned in an array. You can limit the
number of pieces with the optional limit parameter.

To get the domain name from the customer’s email address in the script, you can use the following
code:

$email_array = explode('@', $email);

This call to explode() splits the customer’s email address into two parts: the username, which is
stored in $email_array[0], and the domain name, which is stored in $email_array[1].

You can reverse the effects of explode() by using either implode() or join(), which are identical.
For example,

$new_email = implode('@', $email_array);

This statement takes the array elements from $email_array and joins them with the string passed
in the first parameter. The function call is similar to explode(), but the effect is the opposite.

Substring

The substr() function enables you to access a substring between given start and end points of a
string. It’s not appropriate for the example used here but can be useful when you need to get at
parts of fixed format strings.

The substr() function has the following prototype:

string substr(string string, int start[, int length] );

This function returns a substring copied from within string. The following examples use this test
string:

Compiled By: Ephrem Tesfaye Tsidu 6


Internet Programming II Chapter Five – Strings and Regular Expressions

$test = 'Your customer service is excellent';

If you call it with a positive number for start (only), you will get the string from the start position
to the end of the string. For example,

substr($test, 1);

Returns our customer service is excellent. Note that the string position starts from 0, as with arrays.

If you call substr() with a negative start (only), you will get the string from the end of the string
minus start characters to the end of the string. For example,

substr($test, -9);

Returns excellent.

The length parameter can be used to specify either a number of characters to return (if it is positive)
or the end character of the return sequence (if it is negative). For example,

substr($test, 0, 4);

Returns the first four characters of the string—namely, Your.

Comparing Strings

So far, we’ve just discussed how to use the operator == to compare two strings for equality. You
can do some slightly more sophisticated comparisons using PHP.

The strcmp(), strcasecmp(), and strnatcmp() functions can be used to order strings. This capability
is useful when you are sorting data. The prototype for strcmp() is

int strcmp(string str1, string str2);

The function expects to receive two strings, which it compares. If they are equal, it will return 0.
If str1 comes after (or is greater than) str2, strcmp() will return a number greater than zero(usually
1). If str1 is less than str2, strcmp() will return a number less than Zero(usually -1). This function
is case sensitive.

The function strnatcmp() and its non-case sensitive twin, strnatcasecmp() compare strings
according to a “natural ordering,” which is more the way a human would do it. For example,

Compiled By: Ephrem Tesfaye Tsidu 7


Internet Programming II Chapter Five – Strings and Regular Expressions

strcmp() would order the string 2 as greater than the string 12 because it is lexicographically
greater. strnatcmp() would order them the other way around.

Activity

Read about llexicographical ordering.

Testing String Length

You can check the length of a string by using the strlen() function. If you pass it a string, this
function will return its length. For example, the result of this code is 5:

echo strlen("hello");

Finding Strings in Strings

To find a string within another string, you can use any of the functions strstr() or stristr().

The function strstr(), which is the most generic, can be used to find a string or character match
within a longer string. Stristr() does the same thing but in case sensitive manner.

Finding the Position of a Substring

The functions strpos() and strrpos() operate in a similar fashion to strstr(), except, instead of
returning a substring, they return the numerical position of the string. The PHP manual
recommends using strpos() instead of strstr() to check for the presence of a string within a string
because it runs faster.

For example, the following code echoes the value 4 to the browser:

$test = "Hello world";

echo strpos($test, "o");

Activity

Refer to the rest built in string manipulation functions at https://www.w3schools.com/php/php_ref_string.asp

Compiled By: Ephrem Tesfaye Tsidu 8


Internet Programming II Chapter Five – Strings and Regular Expressions

5.2 Regular Expression in PHP

A regular expression is a way of describing a pattern in a piece of text. Matching regular


expressions in PHP is more like a strstr() match than an equal comparison because you are
matching a string somewhere within another string. (It can be anywhere within that string unless
you specify otherwise.) For example, the string "shop" matches the regular expression "shop". It
also matches the regular expressions "h", "ho", and so on.

You can use special characters to indicate a meta-meaning in addition to matching characters
exactly. For example, with special characters you can indicate that a pattern must occur at the start
or end of a string, that part of a pattern can be repeated, or that characters in a pattern must be of a
particular type. You can also match on literal occurrences of special characters.

Each expression must be contained within a pair of delimiters. You may choose any character that
is not a letter, a number, a backslash, or whitespace. The delimiter at the start and end of the string
must match. The most commonly used delimiter is the forward slash (/). So, for example, if we
wanted to write a regular expression to match the word “shop,” we could write

/shop/

Using character sets immediately gives regular expressions more power than exact matching
expressions. Character sets can be used to match any character of a particular type; they’re really
a kind of wildcard.

First, you can use the (.) character as a wildcard for any other single character except a newline
(\n). For example, the regular expression /.at/ matches the strings "cat", "sat", and "mat", among
others. This kind of wildcard matching is often used for filename matching in operating systems.

With regular expressions, however, you can be more specific about the type of character you would
like to match and can actually specify a set that a character must belong to. In the previous example,
the regular expression matches "cat" and "mat" but also matches "#at".

If you want to limit this to a character between a and z, you can specify it as follows: /[a-z]at/

Compiled By: Ephrem Tesfaye Tsidu 9


Internet Programming II Chapter Five – Strings and Regular Expressions

Anything enclosed in the square brackets ([ and ]) is a character class—a set of characters to which
a matched character must belong. Note that the expression in the square brackets matches only a
single character.

You can list a set; for example, /[aeiou]/ means any vowel.

You can also describe a range, as you just did using the special hyphen character, or a set of ranges,
as follows:

/[a-zA-Z]/ This set of ranges stands for any alphabetic character in upper- or lowercase.

You can also use sets to specify that a character cannot be a member of a set. For example, /[^a-
z]/ matches any character that is not between a and z. The caret symbol (^, sometimes called
circumflex) means not when it is placed inside the square brackets.

Often, you may want to specify that there might be multiple occurrences of a particular string or
class of character.

You can represent this using three special characters in your regular expression. The ∗ symbol
means that the pattern can be repeated zero or more times, and the + symbol means that the pattern
can be repeated one or more times. The ? symbol means that the pattern should appear either once
or not at all. The symbol should appear directly after the part of the expression that it applies to.
For example, /[aeiou]+/ means “at least one vowel character.”

You can specify how many times something can be repeated by using a numerical expression in
curly braces ({}). You can show an exact number of repetitions ({3} means exactly three
repetitions), a range of repetitions ({2, 4} means from two to four repetitions), or an open-ended
range of repetitions ({2,} means at least two repetitions).

For example,

/(very ){1,3}/ matches "very ", "very very ", and "very very very ".

You also can specify whether a particular subexpression should appear at the start, the end, or
both. This capability is useful when you want to make sure that only your search term and nothing
else appears in the string.

Compiled By: Ephrem Tesfaye Tsidu 10


Internet Programming II Chapter Five – Strings and Regular Expressions

The caret symbol (^) is used at the start of a regular expression to show that it must appear at the
beginning of a searched string, and $ is used at the end of a regular expression to show that it must
appear at the end.

For example, the following matches bob at the start of a string:

/^bob/

This pattern matches com at the end of a string:

/com$/

You can represent a choice in a regular expression with a vertical pipe. For example, if you want

to match com, edu, or net, you can use the following expression:

/com|edu|net/

If you want to match one of the special characters mentioned in the preceding sections, such as .,
{, or $, you must put a backslash (\) in front of it. If you want to represent a backslash, you must
replace it with two backslashes (\\).

Be careful to put your regular expression patterns in single-quoted strings in PHP. Using regular
expressions in double-quoted PHP strings adds unnecessary complications.

Let’s take the email format which includes some alphanumeric or punctuation characters, followed
by an @ symbol, followed by a string of alphanumeric and hyphen characters, followed by a dot,
followed by more alphanumeric and hyphen characters and possibly more dots, up until the end of
the string, which encodes as follows:

/^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/

The subexpression ^[a-zA-Z0-9_\-\.]+ means “start the string with at least one letter, number,
underscore, hyphen, or dot, or some combination of those.”

The @ symbol matches a literal @.

The subexpression [a-zA-Z0-9\-]+ matches the first part of the hostname including alphanumeric
characters and hyphens. Note that you slash out the hyphen because it’s a special character inside
square brackets.

Compiled By: Ephrem Tesfaye Tsidu 11


Internet Programming II Chapter Five – Strings and Regular Expressions

The \. combination matches a literal dot (.).

The subexpression [a-zA-Z0-9\-\.]+$ matches the rest of a domain name, including letters,
numbers, hyphens, and more dots if required, up until the end of the string.

A bit of analysis shows that you can easily produce invalid email addresses that will still match
this regular expression. It is almost impossible to catch them all, but this will improve the situation
a little. You can refine this expression in many ways. You can, for example, list valid top-level
domains (TLDs). Be careful when making things more restrictive, though, because a validation
function that rejects 1% of valid data is far more annoying than one that allows through 10% of
invalid data.

Now that you have read about regular expressions, you’re ready to look at the PHP functions that
use them.

Finding Substrings with Regular Expressions

Finding substrings is the main application of the regular expressions you just developed. There are
a number of regular expression functions in PHP. The simplest, preg_match(), is the one we will
use in this example. The preg_match() function returns 1 if a match was found, 0 if a match was
not found, and FALSE if an error occurred. This means you need to use identity (===) to test what
is returned, to avoid confusion between 0 and FALSE.

if (preg_match('/^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/', email) === 0)


{

echo "<p>That is not a valid email address. Please return to the previous page and try
again.</p>";

Replacing Substrings with Regular Expressions

You can also use regular expressions to find and replace substrings in the same way as you used
str_replace(), using the function preg_replace(). This function has the following prototype:

preg_replace(string pattern, string replacement, string subject)

Compiled By: Ephrem Tesfaye Tsidu 12


Internet Programming II Chapter Five – Strings and Regular Expressions

This function searches for the regular expression pattern in the subject string and replaces it with
the string replacement.

Activity

Refer to the rest built in RegEx functions at https://www.w3schools.com/php/php_ref_regex.asp

_______________The End! _______________

Compiled By: Ephrem Tesfaye Tsidu 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy