upd. with Latin as well as with Cyrillic
I have a code that works fine with Latin characters, but when utf-8 removes the first letter of the word, Array ([0] => hello [1] => imon [2] => orobka)

$capitals = array("Π›","К"); $word_break = "ΠΏΡ€ΠΈΠ²Π΅Ρ‚Π›ΠΈΠΌΠΎΠ½ΠšΠΎΡ€ΠΎΠ±ΠΊΠ°"; $word_break = str_replace($capitals, '/',$word_break); 

please tell me how to be without regular expressions

  • one
    And why without regular expressions? - Edward
  • one
    Why write lies? This code will work with the Latin in the same way. Because stupidly replaces the specified letters with a slash. - Ipatiev
  • @ Ipatiev, why should I write lies?) With a Latin alphabet, the array is filled with words from the first letter, nothing is cut off. - Nastya
  • @ Ipatiev here is a pancake, yes, I apologize - Nastya

2 answers 2

Of course, you should do this on regulars, but if you really want the option without them:

 $capitals = array("Π›","К"); $word_break = "ΠΏΡ€ΠΈΠ²Π΅Ρ‚Π›ΠΈΠΌΠΎΠ½ΠšΠΎΡ€ΠΎΠ±ΠΊΠ°"; foreach($capitals as $e){ $word_break=str_replace($e, '/'.$e, $word_break); } 

    Your example can be slightly modified:

     $capitals = array("~(Π›)~", "~(К)~"); $word_break = "ΠΏΡ€ΠΈΠ²Π΅Ρ‚Π›ΠΈΠΌΠΎΠ½ΠšΠΎΡ€ΠΎΠ±ΠΊΠ°"; $word_break = preg_replace($capitals, '/$1', $word_break); echo $word_break; // ΠΏΡ€ΠΈΠ²Π΅Ρ‚/Π›ΠΈΠΌΠΎΠ½/ΠšΠΎΡ€ΠΎΠ±ΠΊΠ° 

    And if the binding is needed not to specific letters, but to all capital letters, then you can:

     $word_break = "ΠΏΡ€ΠΈΠ²Π΅Ρ‚Π›ΠΈΠΌΠΎΠ½ΠšΠΎΡ€ΠΎΠ±ΠΊΠ°"; $word_break = preg_replace('~[А-ЯЁ]~u', '/$0', $word_break); echo $word_break; // ΠΏΡ€ΠΈΠ²Π΅Ρ‚/Π›ΠΈΠΌΠΎΠ½/ΠšΠΎΡ€ΠΎΠ±ΠΊΠ°