PHP – 获取文本的前两句话?
发布时间:2020-12-13 13:11:04 所属栏目:PHP教程 来源:网络整理
导读:我的变量$content包含我的文本.我想从$content创建一个摘录并显示第一个句子,如果句子短于15个字符,我想显示第二个句子. 我已经尝试从文件中删除前50个字符,它可以工作: ?php echo substr($content,50); ? 但我对结果不满意(我不希望任何词语被削减). 是否
我的变量$content包含我的文本.我想从$content创建一个摘录并显示第一个句子,如果句子短于15个字符,我想显示第二个句子.
我已经尝试从文件中删除前50个字符,它可以工作: <?php echo substr($content,50); ?> 但我对结果不满意(我不希望任何词语被削减). 是否有PHP函数获取整个单词/句子,而不仅仅是substr? 非常感谢!
我想通了,但它很简单:
<?php $content = "My name is Luka. I live on the second floor. I live upstairs from you. Yes I think you've seen me before. "; $dot = "."; $position = stripos ($content,$dot); //find first dot position if($position) { //if there's a dot in our soruce text do $offset = $position + 1; //prepare offset $position2 = stripos ($content,$dot,$offset); //find second dot using offset $first_two = substr($content,$position2); //put two first sentences under $first_two echo $first_two . '.'; //add a dot } else { //if there are no dots //do nothing } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |