typestar

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

  1. trim strips the padding; strtoupper shouts the result.
  2. str_replace swaps a word; str_contains asks a yes-or-no question.
  3. explode breaks the line into words and implode glues them back.
  4. str_pad dots out a menu line — the classic receipt trick.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Strings & regex, step 13 of 27 in Language basics.

← Previous Next →

String functions in other languages