String processing

Unit: 13 of 19

A skillful manipulation of strings is a very important characteristic of every serious programmer, so in this lesson we will familiarize ourselves with this topic. The simplest text writing functions are print and echo . Their implementation is almost identical, except that echo is slightly faster at runtime. In terms of syntax, it doesn’t matter if you write:

1
2
3
4
5
print "Hello";
sau:
echo "Hello";

When you need to write more complicated text, the printf() function is used , which displays static text and dynamic information, and its application looks like this:

1
printf("Astazi s-au vandut %d produse. ", 100 );

After executing the function, the following text will be written:

100 products were sold today.

Single (‘ ‘) and double (” “) quotes

As we already mentioned, to assign a string value to a variable, it must be enclosed in quotation marks. Certain special characters can also be used in the text you assign to a variable.

Example of adding HTML tags to a text:

1
2
$string = "PHP <br> programming.";
echo $string;

will write the following two lines of text:

PHP
programming.

If we want a string to be written on the page as written in the PHP code, so without the interpretation of HTML tags, we will use the htmlspecialchars() function.

Example:

1
echo htmlspecialchars("PHP <br> programming.");

The result on the page will be as follows:

PHP <br> programming.

The choice of single or double quotes makes a difference when writing variables. The variable written between double quotes will be read, that is, its value will be displayed, while the variable written between single quotes will be treated as some other text.

Let’s see this on some examples.

Example with single quotes:

1
2
$x = 10;
echo 'Test $x';

The result on the page will be: Test $x

Example for double quotes:

1
2
$x = 10;
echo "Test $x";

The result on the page will be: Test 10

If you still want to use double quotes, but inside them to display the variable name or some other special character that should not be interpreted, you can use the escape character (\).

Example:

1
2
$x = 10;
echo "My variable is \$x.";

The result on the page will be: My variable is $x.

The use of the escape character is very useful when double quotes must be inserted in a string, which is displayed on the page.

Example:

1
2
$x = 10;
echo "Hello, \"World\".";

The result on the page will be: Hello, “World”.

Operations on strings

One of the most common operations on strings is concatenation (dot .). In addition to this operator, PHP also contains a large number of built-in functions for working with text. Some of these are:

  • trim( ) – removes empty spaces from the beginning and end of the string;
  • ltrim ( ) – removes empty spaces from the beginning of the string;
  • rtrim ( ) – removes empty spaces from the end of the string;
  • str_word_count ( ) – counts the words searched in the string, but if the optional parameters are additionally set, then some new strings can be created from the search results;
  • strpbrk(string, char) – searches for the char character in the string text and returns the rest of the string after repeating the characters;
  • strtoupper(string) – convert all letters to uppercase;
  • strtolower(string) – convert all letters to lowercase.

Example:

The str_word_count( ) function can be used in several ways. It is also used in conjunction with the write function print_r() , which performs string writing so that the data is readable to the user. Check out the following usage examples:

1
echo str_word_count("Hello World!");

After execution, the function will return the result:

2

which represents the number of words in the string.

The example in which, as the second argument, the number is also included:

1
print_r(str_word_count("Hello World!", 1));

after execution it will write:

1
Array ( [0] => Hello [1] => World )

If the string also contains a special character as the third argument, we also insert this special character within the parameters of the function, as shown in the example below:

1
2
print_r(str_word_count("Hello World!", 1));
print_r(str_word_count("Hello World!", 1, "!"));

After the execution, we can see that if the third argument was not included, respectively if the special character was not included in the string, the string will skip it at the output and write only the string of the other words. If this special character is specified as an argument, the string will also include the special character in the output.

Display of data after program execution:

1
2
Array ( [0] => Hello [1] => World )
Array ( [0] => Hello [1] => World! )

Example:

Now we will demonstrate the use of functions for working with strings in the case of conversion of uppercase or lowercase. Look at this code example:

1
2
3
4
5
6
$str = "PHP is my favorite programming language.";
echo "Without functions : " . $str;
echo "<br />";
echo "Function - strtoupper : " . strtoupper( $str ) ;
echo "<br />";
echo "Function - strtolower: " . strtolower( $str );

The result on the page will be:

Without functions : PHP is my favorite programming language.
Function – strtoupper : PHP IS MY FAVORITE PROGRAMMING LANGUAGE.
Function – strtolower: php is my favorite programming language.

Formatting text display

The functions printf() and sprintf() ensure  the formatting of text and numbers, as well as their combination. The basic syntax of these instructions is:

1
2
printf( "format", $val1, $val2,...);
$newStr = sprintf( "format", $val1, $val2,...);

Both functions format the text based on the format argument , except that the printf() function just displays it, while the sprintf() function puts it in a new variable. The first argument of these functions, “format” , represents formatting instructions. Each formatting statement has the following form:

1
%pad - lungime.dectip

where:

  • pad – is the character used to fill in free spaces, if the string is shorter than the specified length;
  • the ‘ -‘ sign – means to align characters to the left. If omitted, right-align is performed by default;
  • length  – the number of characters used to display the value;
  • .dec – the number of decimal places;
  • type – the type of value represented (s for string , f for float ).

Look at the following examples:

Example 1

1
2
3
$numOfBoys = 3;
$numOfGirls = 2;
printf("%s boys and  %s girls", $numOfBoys, $numOfGirls);

will write the message:

3 boys and 2 girls

Example 2

1
2
3
4
5
6
$price = 30000;
$product = "Samsung TV";
$message1 = sprintf("%s costs %09.2f dollars. ", $product, $price);
$message2 = sprintf("%'.-20s%9.2f", $product, $price);
echo $message1;
echo $message2;

will write the message:

Samsung TV costs 030000.00 dollars.
SamsungTV……….. 30000.00

In the first message, the first formatting statement is %s , which refers to the first variable,  $product . This statement will only write the value of the variable. The second formatting statement is  %09.2f  and refers to the second variable,  $price . The instruction says that the number must be represented by 9 characters and 2 decimals, where the character 0 is the character with which the empty spaces will be filled.

In the second message, the first instruction to format the product name is %’.-20s . This says that the $product variable should be left-aligned and twenty characters long, where empty spaces will be filled with periods. The price is displayed as in the first message, with the difference that now the character that fills the empty spaces is not specified, which is why the default  space character is used .

When you want to replace strings, use the substr_replace() function,  which has the basic form:

substr_replace(Variable, “characters whose string is changed”, the index of the initial character, the number of characters to be replaced)

Example of use:

1
2
$numbers = "123456789123456789";
echo substr_replace($numbers, "#########",2,8);

After execution, it will be written:

12#########23456789

 

Exercise 1

A program must be created that will transfer the bank account number from written form to electronic form.

In written form, the bank account looks like this:  20012345678
In electronic form, the bank account looks like this: 200000000012345678

Write a function that as an input parameter will take the account number in written form, and instead of calling it will display the account in electronic form.

Solution:

1
2
3
4
5
6
7
8
9
10
<?php
function convertBankAccount($account){
$elecAccount = substr_replace($account,'0000000',3,0);
return $elecAccount;
}
echo convertBankAccount(30012345678);
?>

The key line of this program is:

1
$elecAccount = substr_replace($account,'0000000',3,0);

With the help of substr_replace, we access the account string, and in the quotes we define the characters that change the string; in our case, these are seven figures. The change starts at index 3 and then we specify 0 as the number of characters to be replaced, because we don’t want to make changes, just add elements to the string.

Exercise 2

The application includes the following variables:

$userName and $password

These variables must be checked for validity and ensure that they are not empty and do not contain the following characters: < > i ‘

If one of the variables is empty, the execution is interrupted, while in the case of the existence of unwanted characters, these characters are removed from the values ​​of the variable.

The initial values ​​of the variables should be:

1
2
$userName = "myName<";
$password = "myPa>sswo'rd";


Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
function clean_string($string)
{
    $string = str_replace("<", "", $string);
    $string = str_replace(">", "", $string);
    $string = str_replace("'", "", $string);
    return $string;
}
$userName = "myName<";
$password = "myPa>sswo'rd";
$userName = clean_string(trim($userName));
$password = clean_string(trim($password));
if(trim($userName) == "" || trim($password) == "")
    die("invalid credentials");
echo "valid credentials";
?>

Writing to output:

1
valid credentials

The initial values ​​for the $userName and $password variables are given in the exercise requirements. To process these values, we’ll create a user-defined function that aims to deal with just this. We will call the function clean_string. Within the function above the parameter, we call the already known str_replace function. We will call this function three times to replace each of the characters required in the exercise requirements. After these changes, the function returns the processed string.

When we call the clean_string function, we pass as an argument the value returned by the built-in trim function, after we pass it the string being processed as an argument.

At the end, we check if the strings are empty and, if so, we break the code:

1
die("invalid credentials");


Exercise 3

The following string is given:

1
$lipsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. ";

The text must be formatted so that a new line is formed after every fiftieth character:

Solution 1:

This solution only works for controls that have the possibility to recognize a new line in the text:

1
2
$lipsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
echo wordwrap($lipsum, 15, "<br>");


Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$lipsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$lipsumArr = explode(" ", $lipsum);
$tmpContent = "";
for( $i = 0; $i < sizeof( $lipsumArr ); $i++ )
    {
        if((strlen($tmpContent . " ") + strlen($lipsumArr[$i])) < 15)
            $tmpContent .= " " . $lipsumArr[$i];
        else
            {
                if($tmpContent != "")
                    {
                        echo $tmpContent . "<br>";
                    }
                        $tmpContent = $lipsumArr[$i];
            }
    }
echo $tmpContent;

$lipsum is a variable that contains the text that will be processed. Since we don’t want to do a newline from the middle of the word, we need to separate the entire string into string elements. This is exactly what we do with the explode() method, which accepts two parameters. The first parameter is a separator, while the second parameter is a string to be processed. Now we have prepared a string of words.

We create the variable $tempContent, which will temporarily hold the string that will be displayed on the page. Using the for loops in the manner established so far, we go through all the elements of the string (all the words).

Within the loop, we check if the number of characters of the string, which currently represents the content of the $tmpContent variable, after concatenation with the space and after addition with the number of characters of the word in the string, corresponding to the iteration, is less than 15. If the condition is met, it means that this word can fit in a line and it should be checked if it is possible to enter another word in the same line.

If this condition is not met, it means that there is no space for this word in the line and that line must be written on the page.

Exercise 4

The following string is given:

1
$lipsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";

The string must be formatted in such a way that, if it has more than 15 characters, it is truncated, so that the last three characters, up to the 15th, are periods. E.g:

Lorem Ipsum …

Solution:

1
2
3
4
5
<?php
$lipsum = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$iesire = (strlen( $lipsum ) > 15) ?substr( $lipsum, 0, 12 )."...": $lipsum;
echo $iesire;
?>

The theme here is a simple example of using the  substr() function , which works with three parameters. The first parameter that is approached is the string, the second parameter is the initial index for cutting the string, while the third, the number of characters that will be taken into account, starting with the initial index. The ternary operator is also used. In the parentheses for this operator, we put the condition, followed by the question mark and two possible values ​​for the string. Strings are separated with a colon (:).

After executing this operator, the page should display the value of the variable that saves the output. 

Exercise 5

You must write a program that, for the specified string, will return the number of words in it; the string variable has the following content:

„Lorem Ipsum is simply dummy text of the printing and typesetting industry.”

 

Solution:

1
echo str_word_count("Lorem Ipsum is simply dummy text of the printing and typesetting industry.");

The solution after executing the program is 12!

The purpose of this exercise is to present an interesting function, str_word_count, which accepts one required parameter and two optional parameters. The function counts the words in the string. By defining another parameter, the function can be asked to return a specific result.

Possible values ​​for the second parameter:

  • 0 – Default value. Returns the number of words in a string.
  • 1 – Returns a string that, as elements, contains words from the given string.
  • 2 – Returns a string of words where the keys of the position are in the string.

Exercise 6

Two variables enter the application:

1
2
$numberOfCharacters = 50;
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

You need to create an application that, based on the set of characters and the number, generates a password. The password must contain uppercase and lowercase letters from the $characters list. Also, the password must have as many characters as specified in the $numberOfCharacters variable.

The password must be issued upon exit.

Solution:

1
2
3
4
5
6
7
8
9
<?php
$numberOfCharacters = 50;
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$allCharacters = $characters . strtolower($characters);
$pass = "";
for($i=0;$i<$numberOfCharacters;$i++)
    $pass.=$allCharacters[rand(0,strlen($allCharacters)-1)];
echo $pass;
?>

Solution explanation:

We create a variable by which we specify the length of the password that we will generate ($numberOfCharacters). We then create a character database by defining the $characters variable. Since the value of this variable consists only of uppercase letters, all lowercase letters should be added. In order not to write all these characters, we use the strtolower() function, which compiles all the letters in a string into lowercase and associates the result of this function with the other characters, after which we finally get the $allCharacters variable. The future password will be placed in the $pass variable, which is why we create this variable as an empty string. Passing through the for loop for each iteration, we access the random character of the $allCharacters string, which we obtain using the rand() function. After the loop passes the last iteration,

 

After executing the following code: $name = \”James\”; echo \’My name is $name\’; , the result on the page will be: