Question:
I'm trying to split the following string
Eu irei amanhã à casa. E tu vens?
To get the following result inside an array in php
array(
[0] => eu
[1] => irei
[2] => amanhã
[3] => à
[4] => casa
[5] => .
[6] => E
[7] => tu
[8] => vens
[9] => ?
)
I appreciate any help.
Answer:
If they were just spaces, it would be a case of
$partes = explode( ' ', $todo );
A solution, depending on what you want, would be to force a space before the characters you want to treat as isolated:
$todo = str_replace( array( '.', ',' ,'?' ), array( ' .', ' ,', ' ?'), $todo );
$partes = explode( ' ', $todo );
See it working in IDEONE .
Note that I put the valid separators directly in replace, but if you want to do it with a series of characters, it makes up for a more complex function.
If you prefer to consider all alphanumerics separate from symbols, you can use a RegEx , and solve it in one line:
preg_match_all('~\w+|[^\s\w]+~u', $todo, $partes );
See it working in IDEONE .
Also, it would be the case to add spaces before and after the symbols, remove double spaces, depending on the criterion. The intention of the answer was only to give an initial direction.