Logo
/
Programming
/
PHP Course
/

Data Aggregation (Strings)

PHP: Data Aggregation (Strings)

Aggregation applies not only to numbers but also to strings. These are tasks in which a string is formed dynamically — that is, you don't know in advance what size it will be or what it will contain.

Imagine a function that can multiply a string, that is, repeat it a specified number of times:

<?php

repeat('hexlet', 3); // 'hexlethexlethexlet'

The principle behind this function is quite simple — in a loop, the string is "built up" the specified number of times:

<?php

function repeat(string $text, int $times): string
{
    // The neutral element for strings is an empty string
    $result = '';
    $i = 1;

    while ($i <= $times) {
        // Each time we add the string to the result
        $result = "{$result}{$text}";
        $i = $i + 1;
    }

    return $result;
}

Let's break down the execution of this code step by step:

<?php

// To call repeat('hexlet', 3);
$result = '';
$result = "{$result}hexlet"; // hexlet
$result = "{$result}hexlet"; // hexlethexlet
$result = "{$result}hexlet"; // hexlethexlethexlet

Visually, the process of building up the string looks like this:

repeat('hexlet', 3):

i=1: result = ''             . 'hexlet' = 'hexlet'
i=2: result = 'hexlet'       . 'hexlet' = 'hexlethexlet'
i=3: result = 'hexlethexlet' . 'hexlet' = 'hexlethexlethexlet'
                                            └── result

Neutral element

For the building up to work, you need a starting value. For strings, this value is the empty string ''.

It is called the neutral element because it changes nothing during concatenation:

<?php

print_r('' . 'abc');  // => abc
print_r('abc' . '');  // => abc

That is why the empty string is always used as the initial value when aggregating strings.

Instructions

Implement the sanitizePhoneNumber() function, which takes a phone number from a form and returns a string without spaces, parentheses, or hyphens.

Users enter numbers in different ways, but before saving they are often normalized to a single format. Go through the original string character by character and assemble a new number from useful characters only.

<?php

sanitizePhoneNumber('+7 (999) 123-45-67'); // '+79991234567'
sanitizePhoneNumber('8 800 555 35 35');    // '88005553535'
sanitizePhoneNumber('(123) 456-7890');     // '1234567890'

Hints

  • The final result is a string.
  • Use an empty string as the initial value.
  • The str_contains() function will help you check whether a character is in the set ' ()-'.

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!