I am trying to parse a simple text document in PHP, but have no idea how to do this correctly. I want to separate each word, assign them an identifier and save the result in JSON format.
Sample text:
"Hello, how are you (today)"
This is what I am doing at the moment:
$document_array = explode(' ', $document_text); json_encode($document_array);
Received JSON
[["Hello,"],["how"],["are"],["you"],["(today)"]]
How to ensure that spaces remain in place and that characters are not included with words ...
[["Hello"],[", "],["how"],[" "],["are"],[" "],["you"],[" ("],["today"],[")"]]
I'm sure some kind of regular expression is required ... but I donβt know which template to use to solve all cases ... Any suggestions guys?
Eric Franklin
source share