I woke up last night with a thought in my head: Can PHP be used to generate random words that sound natural? (Like the Lorem ipsum verses).
The purpose would be to fill space on website templates with this instead of 'Lorem ipsum', or send test emails for certain PHP scripts to make sure mail() works.
But my thoughts on how it would work are that PHP would generate random length words, 1-6 letters each, with a few "don't do this" rules like "no two single-letter words next to each other" or "no three-vowels in a row" or "no three-consonants in a row" and automatically add punctuation and capitalization after between 4 and 8 words to a sentence.
Would this be at all possible, and if so, are there any pre-existing classes or functions I could implement?
You can take the context-free grammar approach: http://en.wikipedia.org/wiki/Context-free_grammar
<word> := <vowel> | <consonant><remaining word following consonant> | <vowel><remaining word following vowel>
<vowel> := a|e|i|o|u
<consonant> := b|c|d|f|g|...
<word following vowel> := <consonant><remaining word following consonant>
...and so on
Implement that grammar in any procedural language (C and PHP included), then start generating words based on the grammar.
I don't know of any generic PHP parsing frameworks but you can look at best practices for writing them: Best practices for writing a programming language parser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With