php更改连接语句中的大小写/标点符号


php Changing case/punctuation in concatenated sentences

下面的代码从$sentences数组中生成了一些随机的"诗歌"。大约一半的句子以分号结尾;在这种情况下,我想将下面连接句子的第一个字母改为小写。如果诗中最后一个标点符号是分号,我想把它改成句号。

感谢您提供的任何代码或建议!

<?php 
$sentences = array (
"Lorem ipsum dolor sit amet; ",
"Consectetur adipisicing elit; ",
"Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua; ",
"Ut enim ad minim veniam; ",
"Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat; ",
"Duis aute irure dolor in reprehenderit in voluptate; ",
"Velit esse cillum dolore eu fugiat nulla pariatur; ",
"Excepteur sint occaecat cupidatat non proident; ",
"Sunt in culpa qui officia deserunt mollit anim id est laborum; ",
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium; ",
"Totam rem aperiam; ",
"Eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo; ",
"Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit. ",
"Sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. ",
"Neque porro quisquam est. ",
"Qui dolorem ipsum quia dolor sit amet. ",
"Consectetur, adipisci velit. ",
"Sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. ",
"Ut enim ad minima veniam. ",
"Quis nostrum exercitationem ullam corporis suscipit laboriosam. ",
"Nisi ut aliquid ex ea commodi consequatur? ",
"Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur. ",
"Vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? ",
);
$lastlen = null;
while ($lastlen == null OR ($wrapsize - $lastlen) > ($wrapsize / 10) /* OR $leftover > 0 */ )
{ 
    $pick = rand(7,14);
    shuffle ($sentences);
    $key = 0;
    $fullstring = '';
    while ($key < $pick) 
        { $fullstring .= $sentences[$key];
            $key++; } 
    $wrapsize = rand (20, 70);
    $poemtext = wordwrap($fullstring, $wrapsize, "'n");
    $lines = explode ("'n", $poemtext);
    $count = count($lines);
    $lastkey = $count - 1 ; $lastline = $lines[$lastkey]; 
    $lastlen = strlen($lastline);
    $stanza_size = rand(2,4);
    $leftover = $count % $stanza_size;
    $key = 0; $i = 1;       
    $final = '';
    while ($key < $count) {
        if($i > 0 && $i % $stanza_size == 0) 
        {
        $final .= $lines[$key]."<br><br>";
        $key++; $i++;
        }
        else {
        $final .= $lines[$key]."<br>";
        $key++; $i++;
        }
    } 
}
echo $final;
?>

这样的东西怎么样?

// Boolean to tell us when to make lowercase
$makeLowerCase = false;
while ($key < $pick) 
{
    // Grab the random sentence from the array
    $randomPick = $sentences[$key];
    // If we need to make lowercase the first letter, then do so now
    if($makeLowerCase)
    {
        $randomPick = lcfirst($randomPick);
    }
    // If the end of the random sentence contains a semi-colon, then
    // tell the loop to capitalize the next sentence
    $posSC = strpos($randomPick, ";");
    if($posSC !== false)
    {
        $makeLowerCase = true;
    }
    $fullstring .= $randomPick;
    $key++;
} 
// Replace the final character with a period if it's a semi-colon
$lastChar = substr($fullstring, -1);
if(lastChar == ";")
{
    $finalPoem = substr($fullstring, 0, -1) . ".";
}