Welcome! In this unit, we will dive into an engaging task that tests your PHP programming skills. We will explore string manipulation by parsing strings and performing arithmetic operations on extracted data. Let's get started!
Our task for today involves creating a PHP function called parseAndMultiplyNumbers()
. This function is designed to accept a string as input. However, it's not just any string — this input will include a playful mix of numbers and words.
The purpose of the function is to analyze the input string, extract all the numbers, convert these numbers (currently in string form) into integer data types, and then multiply all these numbers together. The final output will be the product of all those numbers!
Here's an illustration: Given the input string "I have 2 apples and 5 oranges"
, our function should return the product of 2 and 5, which is 10.
The primary task is to parse the string and identify the numbers. We'll start by creating an empty string, $num
, to accumulate digits, and an empty array to collect all the numbers we find:
php1$inputString = "I have 2 apples and 5 oranges"; 2$num = ""; 3$numbers = [];
Next, we need to iterate through the input string character by character. When we encounter a digit, we append it to our $num
string. If a character isn’t a digit and $num
isn’t empty, it means we've reached the end of a number.
At this point, we convert $num
to an integer, add it to the numbers
array, and reset $num
to an empty string. If the character isn’t a digit and $num
is empty, we simply skip and continue.
php1for ($i = 0; $i < strlen($inputString); $i++) { 2 $ch = $inputString[$i]; 3 if (ctype_digit($ch)) { 4 $num .= $ch; 5 } elseif (!empty($num)) { 6 $numbers[] = intval($num); 7 $num = ""; 8 } 9} 10// After the loop, check if 'num' is not empty 11// because it indicates that the last part of the string contains a number. 12if (!empty($num)) { 13 $numbers[] = intval($num); 14} 15foreach ($numbers as $number) { 16 echo $number . " "; 17}
After running this code, the output should be 2 5
.
Finally, we multiply all the numbers in the numbers
array together. The multiplication result gets stored in the $result
variable.
php1$result = 1; 2foreach ($numbers as $number) { 3 $result *= $number; 4} 5echo $result;
After executing this code, the output should be 10
.
Bringing together all the steps, our final PHP solution appears as follows:
php1<?php 2 3function parseAndMultiplyNumbers($inputString) { 4 $num = ""; 5 $numbers = []; 6 7 for ($i = 0; $i < strlen($inputString); $i++) { 8 $ch = $inputString[$i]; 9 if (ctype_digit($ch)) { 10 $num .= $ch; 11 } elseif (!empty($num)) { 12 $numbers[] = intval($num); 13 $num = ""; 14 } 15 } 16 if (!empty($num)) { 17 $numbers[] = intval($num); 18 } 19 20 $result = 1; 21 foreach ($numbers as $number) { 22 $result *= $number; 23 } 24 25 return $result; 26} 27 28// Call the function 29echo parseAndMultiplyNumbers("I have 2 apples and 5 oranges");
This solution also handles numbers located at the end of the input string.
Congratulations! You've successfully developed a PHP function that navigates strings to identify numbers, performs data type conversion, and then executes an arithmetic operation with those numbers. You have demonstrated strong capabilities in orchestrating these coding concepts within PHP.
To further enhance your skills, consider performing different operations on the numbers or changing the criteria for identifying valid numbers, continuing to build upon your PHP expertise. Here's to continued success in coding!