3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); mb_internal_encoding('utf-8'); error_reporting(E_ALL); $text1 = "ну что. не смотрел еще black mesa?я собирался скачать ,но все как-то некогда было!"; $text2 = " roses are red,and violets are blue.whatever you do I'll keep it for you."; $text3 = "привет.есть 2 функции,preg_split и explode ,не понимаю,в чем между ними разница."; $text4 = "много их в Петербурге,молоденьких дур,сегодня в атласе да бархате,а завтра , поглядишь , метут улицу вместе с голью кабацкою...в самом деле ,что было бы с нами ,если бы вместо общеудобного правила:чин чина почитай , ввелось в употребление другое,например:ум ума почитай?какие возникли бы споры!"; function makeFirstCharInSentenceUpperCase(string $sentence): string { $firstChar = mb_substr($sentence, 0, 1); $remainingSentence = mb_substr($sentence, 1); $upperFirstChar = mb_strtoupper($firstChar); return $upperFirstChar . $remainingSentence; } /** * @param string $sentence * * @return string * @throws RuntimeException */ function rearrangeSpacesInSentence(string $sentence): string { $rearrangedSentence = preg_replace( ['/\s\s+/', '/\s([.,:;!?])/', '/([.,:;!?])(\w)/u'], [' ', '$1', '$1 $2'], $sentence ); if (null === $rearrangedSentence) { throw new RuntimeException(preg_last_error_msg()); } return $rearrangedSentence; } /** * @param string $text * @param array<callable(string):string> $rules * * @return string * @throws RuntimeException */ function fixSentencesInTextByRules(string $text, array $rules): string { $sentences = preg_split('/(?<=[.!?])(?=[\w\s])/u', $text, -1, PREG_SPLIT_NO_EMPTY); foreach ($rules as $rule) { $sentences = array_map($rule, $sentences); } return implode(' ', $sentences); } $texts = [$text1, $text2, $text3, $text4]; $sentenceRules = [ trim(...), makeFirstCharInSentenceUpperCase(...), rearrangeSpacesInSentence(...), ]; foreach ($texts as $text) { echo fixSentencesInTextByRules($text, $sentenceRules), "\n"; }

preferences:
57.01 ms | 414 KiB | 5 Q