Strings Checkpoint

Strings Checkpoint

Playground Checkpoint

Fix the reminder script — names cut by bytes, phone numbers in five formats, prices without decimals — until the output matches the expected output.

25–35 min

The Challenge

Olivia wants the reminder script finished before Tom's next test evening. Each reminder needs three things. A name that fits the SMS: at most 10 characters, shortened with "…", and never cut in the middle of a letter. A phone number in one format, +44 and ten digits — or no SMS at all if the number isn't valid. And a price with two decimals.

Open the starter in the PHP Playground with the button below. It has the old code: smsName() with substr(), cleanPhone() with str_replace(), and a message glued together with dots. Run it, compare its output with the expected output in the comments, and fix it with what you learned in this Act.

What You Need to Do

  • Run the starter and find every wrong piece of the output. For each one, note which chapter explains it.
  • Fix smsName() so it counts characters, not bytes. Names of 10 characters or fewer stay as they are; longer names are shortened to 10 characters, including the "…" (chapter 6).
  • Rewrite cleanPhone() as clean, check, one format: remove everything except digits and +, check the rest with one pattern anchored with ^ and \z, and return +44 and the ten digits — or null (chapters 7 and 8).
  • Build the message with sprintf() or a heredoc, with the price shown with two decimals (chapters 2 and 4).
  • Don't change the bookings or the lines under "Send the reminders". The output must match the expected output exactly, with no warnings.
Stuck? A Few Hints
  • strlen() and substr() count bytes, and è, ë and ø take two bytes each. mb_strlen() and mb_strimwidth() count characters (chapter 6).
  • mb_strimwidth($name, 0, 10, "…") keeps the result within 10 characters, including the "…".
  • Clean with preg_replace('/[^\d+]/', '', $raw). Then check with '/^(?:\+44|0044|0)(7\d{9})\z/' and build the number from the captured group (chapter 8).
  • sprintf("Total: £%.2f", $price) shows 25 as £25.00 and 27.5 as £27.50 (chapter 4).

Ready to Build It?

Opens the Playground, right in your browser — nothing to install.

Open Playground

A correct run prints six lines: five reminders, each starting with a number in the form +447700900…, with the names "Tom Hart", "Clare Lef…", "Zoë Müller", "Søren Øde…" and "Daniel Br…" and every price with two decimals — then "SKIPPED Ann Lee: invalid number 0770 090 012". No warnings. The starter prints a broken name, a name cut although it fits, phone numbers in five different shapes (one of them breaks its line in two), "at9:30" and "£27.5".

Before You Move On

If your output matches, every reminder has a whole name, a number the SMS service accepts and a price that looks like a price. Along the way you've used most of PHP's string toolbox: quotes and interpolation, heredoc, searching and replacing, sprintf(), safe comparison, the mb_ functions and regular expressions. Next: arrays — and a schedule where 10:00 comes before 9:00.