String functions in PHP
The everyday string toolkit: trim, replace, split and join.
<?php
$line = " The Quick Brown Fox ";
$clean = trim($line);
echo strtoupper($clean), "\n";
echo str_replace("Quick", "Sly", $clean), "\n";
echo strlen($clean), "\n";
echo str_contains($clean, "Brown") ? "found" : "missing", "\n";
$words = explode(" ", strtolower($clean));
echo implode("-", $words), "\n";
echo ucfirst("php strings"), "\n";
echo str_pad("menu", 10, "."), "\n";
How it works
trimstrips the padding;strtouppershouts the result.str_replaceswaps a word;str_containsasks a yes-or-no question.explodebreaks the line into words andimplodeglues them back.str_paddots out a menu line — the classic receipt trick.
Keywords and builtins used here
echoexplodeimplodestr_containsstr_padstr_replacestrlenstrtolowerstrtouppertrimucfirst
The run, in numbers
- Lines
- 13
- Characters to type
- 382
- Tokens
- 122
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 98 seconds.
Step 1 of 3 in Strings & regex, step 13 of 27 in Language basics.