Search for a Word in a Text File Using PHP | Find All Occurrences with Line Numbers

Опубликовано: 25 Июль 2026
на канале: MS Mthethwa
27
0

In this PHP tutorial, we’ll write a script to search for a specific word — “example” — in a text file named notes.txt. The script will:
✅ Read the entire file line by line
✅ Search for all instances of the word, regardless of how many times it appears
✅ Display the line number(s) where the word is found

📚 This is a great exercise in:
File handling (fopen, fgets)
String search (strpos)
Looping through files
Displaying search results dynamically

💡 Perfect for beginner to intermediate PHP learners looking to work with real file data and search logic!