PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php function reverseWordsInSentence($sentence) { $words = preg_split('/(\s+|[.,!?-])/', $sentence, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $reversedWords = array_map(function($word) { if (preg_match('/[.,!?-]/', $word)) { return $word; } else { return mb_strrev(preg_replace('/[.,!?-]/u', '', $word), 'UTF-8') . preg_replace('/\p{L}+/u', '', $word); } }, $words); return implode('', $reversedWords); } function mb_strrev($string, $encoding = 'UTF-8') { $length = mb_strlen($string, $encoding); $reversed = ''; while ($length-- > 0) { $reversed .= mb_substr($string, $length, 1, $encoding); } return $reversed; } $inputSentence = "Куда идём мы с Пятачком - Большой-большой секрет!"; $outputSentence = reverseWordsInSentence($inputSentence); echo $outputSentence;
Show:  
Copy Clear