It is required to write the following logic:

Remove from the string all characters except the Latin / Cyrillic letters and "," (comma), and the "-" sign, if it occurs, should be replaced with a space, if there is already a space between the owls, then it must also be preserved .

It turned out to fulfill the first two conditions, but it’s impossible to find and replace "-" ..

/[^ a-zа-яё,-]/ui 

I would be very grateful for the help

  • one
    In fact, you can replace one character with another using str_replace. - u_mulder

1 answer 1

Make two templates - the first one finds inconsistencies and cuts the excess, the second one adds spaces:

 $str = 'слово-(полслова) слово'; echo preg_replace(['~[^\pL\s,-]+~us', '~-~'], ['', ' '], $str); 
  • Thanks for the response. But so far a little has happened. "-" finds and deletes, but does not replace with spaces .. words stick together .. - Amsterdam
  • Thanks, I understood my mistake in the question, corrected - Amsterdam
  • Sorry, but the space between words after deleting "-" still does not appear. (The space, if it was originally, remains. C it happened.) - Amsterdam
  • @Eduard and try such an example "word-word", you will see that there is no space) - Amsterdam
  • one
    @Edward now works as desired. Great! Thank you so much!) - Amsterdam