【发布时间】:2015-04-30 09:04:36
【问题描述】:
我有一长串文本,如何限制输出以计算句号并只输出到第三个句号?
Ex Raw:
Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商采用了一种类型的厨房并将其加扰以制作类型样本书。它不仅经历了五个世纪,而且经历了电子排版的飞跃,基本保持不变。它不仅经历了五个世纪,而且经历了电子排版的飞跃,基本保持不变。
期望的输出
Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商使用了一种类型的厨房并加扰它来制作类型样本书。
// Long text
$profile_desc = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.';
// Split long text on each full stop.
$profile_desc_out = explode( '. ', $profile_desc );
$i = 0;
// Repeat 3 times to get three sentences
foreach( $profile_desc_out as $profile_desc_output ){
echo $profile_desc_output;
if( ++$i > 3 ) {
break;
}
}
// How to incorporate implode() function to put back together?
【问题讨论】:
-
在停止处拆分(分解)字符串。获得前三个项目。放回原处(内爆)。
-
感谢@GolezTrol,我已经按照你的建议做了,但我不明白如何在这种情况下使用 implode()?
-
implode与explode相反。如果您希望将三个句子合并到一个字符串中,这将很有用。但是由于您立即回显文本,因此没有必要这样做。 -
附言。对于这种情况,经典循环可能更简单:
for ($i = 0; $i < 3; $i++) { echo $profile_desc_out[$i]; }. -
谢谢。我确实想把这三个句子重新组合成一个刺痛。如果我在没有内爆的情况下回显它,它会去掉句号和前面的空格。
标签: php