Data Structures
Strings & Regex
Topic 8 of 13
PHP
<?php
$s = " Hello, World! ";
strlen($s); // length
str_contains($s, 'World'); // PHP 8+
str_starts_with($s, 'Hello');
str_ends_with($s, '!');
trim($s); // strip whitespace
strtolower($s);
str_replace('World', 'PHP', $s);
substr($s, 7, 5); // "World"
explode(',', "a,b,c"); // โ array
implode('-', ['a','b','c']); // "a-b-c"
sprintf("%.2f", 3.14159); // "3.14"
number_format(1234567, 2); // "1,234,567.00"
// Heredoc (parses variables)
$text = <<<EOT
Name: $name
Multiline string here.
EOT;
// Regex (PCRE)
preg_match('/^\d{10}$/', $phone);
preg_match_all('/\b\w+\b/', $text, $matches);
preg_replace('/\s+/', ' ', $text);