php - Inserting spaces between UTF8 words - what is wrong with my 2 simple regexes? -


i have a web page users can leave comments in russian language (utf-8), one:

Хорошо, четко , уверено!Удачи!(БОРИС) 

some users "abuse" fun purposes leaving out spaces between words

НеСпитьсяЖукуНиЗимою,НиЛетом,лучшеПитатьсяСолнечнымСветом, лучшеСидетьЗаИгорнымСтолом,иНаслаждатьсяКаждымВистом, лучшеНоситьЗолотыеОдежды,искритьсяВсегда,неТеряяНадежды,лучшеПустьДругОстаетсяБезВзятки,ведьНевозможноЖукуЖитьБезЛапки! 

which results in wide html-table rows, breaking layout.

i'm trying counter users trying find comments on 60 non-space characters , inserting single space char after punctuation chars (like commas) - piece of php-code:

            if (preg_match('/\s{60,}/u', $about) == 1) {                     error_log('splitting comment: ' . $about);                     $about = preg_replace('/(\p{p}+\s*)/u', '$1 ', $about);                     error_log('===result comment: ' . $about);             } 

however doesn't work , has @ least 2 problems

  1. every comment being matched, short 1 @ top
  2. the \s* isn't greedy , "comma , space" replaced "comma space space" weird reason

here excerpt of log file:

[04-jun-2012 09:50:10] splitting comment: Хорошо, четко , уверено!Удачи!(БОРИС) [04-jun-2012 09:50:10] ===result comment: Хорошо,  четко ,  уверено! Удачи!( БОРИС)  [04-jun-2012 09:50:10] splitting comment: НеСпитьсяЖукуНиЗимою,НиЛетом,лучшеПитатьсяСолнечнымСветом, лучшеСидетьЗаИгорнымСтолом,иНаслаждатьсяКаждымВистом, лучшеНоситьЗолотыеОдежды,искритьсяВсегда,неТеряяНадежды,лучшеПустьДругОстаетсяБезВзятки,ведьНевозможноЖукуЖитьБезЛапки!(nusja) [04-jun-2012 09:50:10] ===result comment: НеСпитьсяЖукуНиЗимою, НиЛетом, лучшеПитатьсяСолнечнымСветом,  лучшеСидетьЗаИгорнымСтолом, иНаслаждатьсяКаждымВистом,  лучшеНоситьЗолотыеОдежды, искритьсяВсегда, неТеряяНадежды, лучшеПустьДругОстаетсяБезВзятки, ведьНевозможноЖукуЖитьБезЛапки!( nusja) 

i tried doubling backslashes - hasn't changed anything.

i'm using stock php latest centos linux 5.x , 6.x:

# php -v php 5.3.3 (cli) (built: may  7 2012 17:58:11) copyright (c) 1997-2010 php group zend engine v2.3.0, copyright (c) 1998-2010 zend technologies 

try wordwrap(). it's built-in function , think should you.

if you're looking reg-exp solution, there's 1 in comments - look here. it's utf-8 safe, should work russian site.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -