@ 2023-05-31T04:56:26Z <?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";
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.33 , 8.2.0 - 8.2.29 , 8.3.5 - 8.3.25 , 8.4.1 - 8.4.12 Ну что. Не смотрел еще black mesa? Я собирался скачать, но все как-то некогда было!
Roses are red, and violets are blue. Whatever you do I'll keep it for you.
Привет. Есть 2 функции, preg_split и explode, не понимаю, в чем между ними разница.
Много их в Петербурге, молоденьких дур, сегодня в атласе да бархате, а завтра, поглядишь, метут улицу вместе с
голью кабацкою... В самом деле, что было бы с нами, если бы вместо общеудобного правила: чин чина почитай, ввелось в
употребление другое, например: ум ума почитай? Какие возникли бы споры!
Output for 8.0.1 - 8.0.29 Parse error: syntax error, unexpected token ")" in /in/hkjXO on line 63
Process exited with code 255 . preferences:dark mode live preview ace vim emacs key bindings
73.56 ms | 408 KiB | 5 Q