🚀 A Clean Pattern for Word-by-Word String Parsing in PHP
Recently, while solving problems like:
- ✅ Counting segments in a sentence
- ✅ Reversing words
- ✅ Manual string tokenization
I discovered a powerful template for breaking down a string into individual words without using built-in functions like explode()
or str_word_count()
.
Here's the simplified version of the logic:
$sentence = [];
$word = '';
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] !== ' ') {
// Keep building the word until space is found
$word .= $s[$i];
} else {
// Space hit = word finished, push it to array
if( $word != '' ){
$sentence[] = $word;
$word = ''; // Reset word builder
}
}
}
// After loop ends, push the last word (if any)
if ($word !== '') {
$sentence[] = $word;
}
🧠 What’s happening here?
- We're looping through each character.
- If the character is not a space → it's part of a word → build it.
- If the character is a space → we finished building one word → store it → reset.
- At the end of the loop, if a word is still in progress, we save it.
💥 Why is this helpful?
- Works even when multiple spaces are between words (after using
preg_replace('/\s+/', ' ', $s)
to normalize). - Doesn't rely on external functions, gives you full control.
- Can be adapted to parse custom delimiters or handle punctuation-sensitive input.
✨ Bonus Insight:
The final word in a string is not followed by a space — so it never hits the “else” block. That’s why the if ($word !== '')
after the loop is crucial. Without it, your last word would be lost!
📌 Template takeaway:
If you're building tools that deal with sentence parsing, custom formatting, or you're preparing for string-related DSA problems, this small but powerful pattern will keep showing up!
Let me know what you think, or how you'd adapt this for other tasks!
Pretty cool seeing someone ditch built-ins and actually walk through it - I always get a kick out of handling stuff character by character.