Question

I need your help in my code:

$sentence = "Erzurum da hayat çok monoton SANIRIM:d";
$words = preg_split('/[\s,.\'\-\[\]]+/', $sentence, -1, PREG_SPLIT_NO_EMPTY);

this code splits the sentence into words and the output is below

Array ( [Erzurum] => 1 [da] => 1 [hayat] => 1 [çok] => 1 [monoton] => 1 [SANIRIM:d] => 1)

But I want to split the ":d" character in the last word How can I do this?

Was it helpful?

Solution

Use a positive look-ahead assertion:

preg_split('/([\s,.\'\-\[\]]+|(?=:))/'

which means "split if the next character is a ':' ". You could also use a look-behind, depending on what you actually intend to use the string for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top