Lab 06: Strings & Regular Expressions
Objective
Manipulate strings with PHP's built-in functions, format output with sprintf, use heredoc/nowdoc, and apply regular expressions with preg_match, preg_replace, and preg_split.
Background
PHP has over 100 string functions and a mature PCRE (Perl Compatible Regular Expressions) engine. String manipulation is at the heart of web development — processing user input, formatting output, parsing logs, validating data, and building HTML/JSON responses.
Time
35 minutes
Prerequisites
Lab 05 (Functions)
Tools
PHP 8.3 CLI
Docker image:
zchencow/innozverse-php:latest
Lab Instructions
Step 1: Essential String Functions
💡
str_contains(),str_starts_with(),str_ends_with()were added in PHP 8.0 — finally replacing the verbosestrpos() !== falsepattern. Always prefer these for readability. Note:strposreturnsfalse(not -1) when not found —strpos($s, 'x') == 0is a bug if 'x' is at position 0!
📸 Verified Output:
Step 2: Substr, Replace & Split
💡
str_padwithSTR_PAD_LEFTis the clean way to zero-pad numbers:str_pad('42', 8, '0', STR_PAD_LEFT)→00000042. More readable thansprintf('%08d', 42)for simple cases.STR_PAD_BOTHcenters the string.
📸 Verified Output:
Step 3: sprintf & Number Formatting
💡
printfformat specifiers:%s= string,%d= integer,%f= float,%e= scientific,%.2f= 2 decimal places,%08d= zero-padded to 8 chars,%-15s= left-aligned in 15 chars.printfprints directly;sprintfreturns the string.
📸 Verified Output:
Step 4: Heredoc & Nowdoc
💡 Heredoc interpolates variables; nowdoc does not — the only difference is the single quotes around the opening label. Indented heredoc (PHP 7.3+) strips leading whitespace up to the closing marker's indentation level, making it practical inside indented code.
📸 Verified Output:
Step 5: Regular Expressions — preg_match
💡
preg_matchreturns 0 or 1 (nottrue/false), and returnsfalseon regex error. Always check forfalseexplicitly in critical code. Capture groups are stored in the third argument$matches— index 0 is the full match, 1+ are groups.
📸 Verified Output:
Step 6: preg_replace & preg_split
💡
preg_replace_callbackis the most powerful replace tool — you get the full match and all capture groups as$matches, and can compute the replacement dynamically. Use it for price updates, template rendering, code highlighting, and any transformation that can't be expressed as a static replacement string.
📸 Verified Output:
Step 7: String Security & Encoding
💡 Never use
md5()orsha1()for passwords — they're fast hashing algorithms, vulnerable to brute force. Usepassword_hash()withPASSWORD_BCRYPTorPASSWORD_ARGON2ID. They're intentionally slow and include a salt automatically.
📸 Verified Output:
Step 8: Real-World — Log Parser
💡
PREG_SET_ORDERorganizes matches so each element is one full match with all its groups —$matches[0]is the first log line with all its captured groups. Without it,$matches[0]would be all full matches,$matches[1]all first-group captures, etc.PREG_SET_ORDERis almost always what you want.
📸 Verified Output:
Verification
Expected: Hello, World, PHP, Land
Summary
PHP's string arsenal is massive. You've covered trimming/case/search functions, sprintf formatting, heredoc/nowdoc, preg_match with capture groups, preg_replace_callback, string security (password hashing, HTML escaping), and a complete log parser. These skills cover 90% of real PHP string work.
Further Reading
Last updated
