doufeiqiong3515 2013-08-12 12:08
浏览 27

将段落中的第一个句子大写

I am using WordPress and WP-O-Matic to automatically pull contents from different feeds. The contents are in all caps making the posts in the WordPress blog looks crappy. I tried using different techniques, but none of them seem to work flawlessly.

Here are some examples I tried:
How to uppercase the first letter in a sentence in php
How to capitalize first letter of first word in a sentence?

I am currently using this piece of code, but its not working like it should:

function fwisc_content($content) {
if ( is_single() ) {
global $post;
$string = get_the_content($post->ID);
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
$new_string = '';
foreach ($sentences as $key => $sentence) {
    $new_string .= ($key & 1) == 0?
        ucfirst(strtolower(trim($sentence))) :
        $sentence.' ';
}
return trim($new_string); 

} else { 
    return $content;
}
}
add_action( 'the_content', 'fwisc_content' );

The problem is this code removes all the <p></p> tags making the whole post looks like a single paragraph.

Here is what I need to do:
Sample Input:

<p>THIS IS THE FIRST SENTENCE. THIS IS THE SECOND SENTENCE! THIS IS THE THIRD SENTENCE.. THIS IS THE FOURTH SENTENCE! IS THIS THE FIFTH SENTENCE?</p>
<p>THIS IS THE FIRST SENTENCE. THIS IS THE SECOND SENTENCE! THIS IS THE THIRD SENTENCE.. THIS IS THE FOURTH SENTENCE! IS THIS THE FIFTH SENTENCE?</p>

Expected Output:

<p>This is the first sentence. This is the second sentence! This is the third sentence.. This is the fourth sentence! Is this the fifth sentence?</p>
<p>This is the first sentence. This is the second sentence! This is the third sentence.. This is the fourth sentence! Is this the fifth sentence?</p>

Please help

  • 写回答

1条回答 默认 最新

  • dongling2545 2013-08-12 12:57
    关注

    Try out the following code:

    <style>
        p:first-letter
        {
        text-transform: uppercase;
        }
    </style>
    <?php
    function upperFirstWord($str) {
        return preg_replace('/([.!?])\s*(\w)/e', "strtoupper('\\1 \\2')", ucfirst(strtolower($str)));
    }
    
    echo upperFirstWord("<p>THIS IS THE FIRST SENTENCE. THIS IS THE SECOND SENTENCE! THIS IS THE THIRD SENTENCE.. THIS IS THE FOURTH SENTENCE! IS THIS THE FIFTH SENTENCE?</p><p>THIS IS THE FIRST SENTENCE. THIS IS THE SECOND SENTENCE! THIS IS THE THIRD SENTENCE.. THIS IS THE FOURTH SENTENCE! IS THIS THE FIFTH SENTENCE?</p>");
    ?>
    

    Hope this will help. Thank you :-)

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch