【发布时间】:2013-08-13 17:48:29
【问题描述】:
我正在使用 WordPress 和 WP-O-Matic 自动从不同的提要中提取内容。内容全部大写,使 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>
请帮忙
【问题讨论】:
-
你能提供一些输入和预期输出吗?另外,您的代码如何删除
<p></p>? -
@HamZa 编辑了我的问题并添加了示例输入和预期输出。
-
@Abhik:您给定的输入中没有
<p></p>。 -
你的 code doesn't remove
<p></p>我从一开始就怀疑。这似乎是一次巨魔尝试。 -
可能是个愚蠢的问题,但
<p>标签是否首先存在,而不是包含在DIVs或<br>标签中?
标签: php wordpress text-segmentation