PHP strings, sprintf, and regular expressions
PHP strings come with a large set of functions. strlen gives the length in bytes, substr extracts part of the string, and strpos finds a position or returns false. explode splits a string into an array and implode joins an array back together. str_contains, ucfirst, strrev, and str_pad cover many everyday tasks.
PHP
<?php
$name = "Programonic";
echo strlen($name), " ", substr($name, 0, 7), " ", strpos($name, "m"), "\n";
echo sprintf("%05.1f|%-8s|%'*8s|%x", 3.14159, "left", "right", 255), "\n";
$parts = explode(",", "a,b,,c");
echo count($parts), " ", implode("-", $parts), "\n";
echo str_contains("haystack", "st") ? "yes" : "no", " ", ucfirst("php"), " ", strrev("abc"), "\n";
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', "2024-03-15", $m)) {
echo $m[1], "/", $m[2], "/", $m[3], "\n";
}
echo preg_replace('/\s+/', ' ', "too many spaces"), "\n";
echo str_pad("7", 3, "0", STR_PAD_LEFT), "\n";