Skip to content

Commit f0c39a8

Browse files
authored
Merge pull request #22 from php-school/new-string-functions
Lord of the strings
2 parents 493a567 + 9c3d655 commit f0c39a8

File tree

21 files changed

+3864
-5
lines changed

21 files changed

+3864
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/program.php
44
/solution
55
/exercises/*/solution/vendor
6+
/test/solutions/*/*/vendor
67
.phpunit.result.cache

app/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpSchool\PHP8Appreciate\Exercise\CautionWithCatches;
2424
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
2525
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
26+
use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
2627
use PhpSchool\PhpWorkshop\Application;
2728

2829
$app = new Application('PHP8 Appreciate', __DIR__ . '/config.php');
@@ -31,6 +32,7 @@
3132
$app->addExercise(HaveTheLastSay::class);
3233
$app->addExercise(PhpPromotion::class);
3334
$app->addExercise(CautionWithCatches::class);
35+
$app->addExercise(LordOfTheStrings::class);
3436

3537
$art = <<<ART
3638
_ __ _

app/config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22

3-
use PhpSchool\PHP8Appreciate\AstService;
43
use PhpSchool\PHP8Appreciate\Exercise\AMatchMadeInHeaven;
54
use PhpSchool\PHP8Appreciate\Exercise\CautionWithCatches;
65
use PhpSchool\PHP8Appreciate\Exercise\HaveTheLastSay;
76
use PhpSchool\PHP8Appreciate\Exercise\PhpPromotion;
7+
use PhpSchool\PHP8Appreciate\Exercise\LordOfTheStrings;
88
use Psr\Container\ContainerInterface;
9+
910
use function DI\create;
1011
use function DI\factory;
1112
use function DI\object;
@@ -26,4 +27,7 @@
2627
CautionWithCatches::class => function (ContainerInterface $c) {
2728
return new CautionWithCatches($c->get(PhpParser\Parser::class), $c->get(\Faker\Generator::class));
2829
},
30+
LordOfTheStrings::class => function (ContainerInterface $c) {
31+
return new LordOfTheStrings($c->get(\Faker\Generator::class));
32+
},
2933
];

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
PHP has a huge standard library with the most extravagant and highly specific string and array functions available.
2+
`levenshtein` & `array_change_key_case` anyone??
3+
4+
On the flip side, historically it has missed some very common string operations. There's always been a way to get the same results, but they are cumbersome and error prone.
5+
6+
For example, if you want to check if a string contains a string you might use `strpos`. Eg:
7+
8+
```php
9+
if (strpos('colourful', 'colour')) {
10+
}
11+
```
12+
Well, this check will fail because `strpos` will return `0` which will be interpreted as false. You will need to adapt that check to account for 0 with `strpos('colourful', 'colour') !== false` where the strict comparison is important.
13+
14+
Thankfully, PHP 8 has done away with all that nonsense. Please welcome the following functions in to the world of PHP:
15+
16+
* `str_contains`
17+
* `str_starts_with`
18+
* `str_ends_with`
19+
20+
----------------------------------------------------------------------
21+
22+
Create a program the accepts two arguments, the first is a word, the second is a sentence (random words).
23+
24+
You must print a table which contains the results of each of the above functions with the provided word and sentence.
25+
26+
* First, does the sentence contain the word?
27+
* Second, does the sentence start with the word?
28+
* Lastly, does the sentence end with the word?
29+
30+
You will use the composer package `symfony/console` to print a table using the `Table` helper.
31+
32+
The table headers should be `Function` & `Result`
33+
34+
There should be three rows, one for each function. The function column should contain the function name, eg `str_contains`.
35+
The result column should be `true` or `false` based on the result of the corresponding function call.
36+
37+
### The advantages of the new string functions
38+
39+
* Simpler and more concise.
40+
* Saner return types.
41+
* It is harder to get their usage wrong, for example checking for 0 vs false with `strpos`
42+
* The functions are faster, being implemented in C.
43+
* The operations require less function calls, for example no usages of `strlen` are required.
44+
45+
----------------------------------------------------------------------
46+
## HINTS
47+
48+
Point your browser to [https://getcomposer.org/doc/00-intro.md](https://getcomposer.org/doc/00-intro.md) which will walk you through **Installing Composer** if you haven't already!
49+
50+
Use `composer init` to create your `composer.json` file with interactive search.
51+
52+
For more details look at the docs for...
53+
54+
**Composer** - [https://getcomposer.org/doc/01-basic-usage.md](https://getcomposer.org/doc/01-basic-usage.md)
55+
**Symfony Console** - [https://symfony.com/doc/current/components/console.html](https://symfony.com/doc/current/components/console.html)
56+
**str_contains** - [https://www.php.net/manual/en/function.str-contains.php](https://www.php.net/manual/en/function.str-contains.php)
57+
**str_starts_with** - [https://www.php.net/manual/en/function.str-starts-with.php](https://www.php.net/manual/en/function.str-starts-with.php)
58+
**str_ends_with** - [https://www.php.net/manual/en/function.str-ends-with.php](https://www.php.net/manual/en/function.str-ends-with.php)
59+
60+
For Symfony Console you will want to look specifically for the Table Helper.
61+
62+
Oh, and don't forget to use the Composer autoloader with:
63+
64+
```php
65+
require_once __DIR__ . '/vendor/autoload.php';
66+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "php8-appreciate/lord-of-the-strings",
3+
"description": "String manipulation with Composer",
4+
"require": {
5+
"symfony/console": "^5"
6+
}
7+
}

0 commit comments

Comments
 (0)