我正在使用WordPress和WP-O-Matic自动从不同的Feed中提取内容。内容全部大写,使得WordPress博客中的帖子看起来很糟糕。我尝试使用不同的技术,但它们似乎都没有完美地工作。
以下是我尝试的一些例子:
How to uppercase the first letter in a sentence in php
How to capitalize first letter of first word in a sentence?
我目前正在使用这段代码,但它不能正常工作:
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' );
问题是此代码删除了所有<p></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>
<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>
<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>
请帮忙
答案 0 :(得分:0)
尝试以下代码:
<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>");
?>
希望这会有所帮助。谢谢: - )