Как использовать preg_replace для удаления повторяющихся символов с пробелами вокругPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как использовать preg_replace для удаления повторяющихся символов с пробелами вокруг

Сообщение Anonymous »


I have multiple strings and I need to remove repeated chars. For example: the string here abbbbbc x should become here abc x or the string test jjka should become test jka.

After studying, I came up with the code below which works fine (it uses PHP but you can use any language):

echo preg_replace("/([a-z])\\1+/","$1","test ajjjo new"); The code above will output test ajo new which is great!

My problem now, is that I need to only replace the repeated chars if they are inside a word or at the beggining of end of the word. For example: I need the string here bbb cca to become here bbb ca and the string test hjjjja ppp to become test hja ppp. I tried negating the (space) and ^ and $ but it all becomes a mess pretty fast.

How would you recommend me?


Источник: https://stackoverflow.com/questions/780 ... ces-around
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»