gpt4 book ai didi

接近 100 个单词的句号后分页的 Php 代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:26:13 26 4
gpt4 key购买 nike

我有一个测试区,它是来自数据库的动态文本,可能有数千个单词。我希望它中断并在每段中用句号将近 100 个(任意长度)单词作为一个段落。我能够在 100 个单词后中断,但不能完全停止。为了在 100 个(任意长度)单词后中断,我使用了以下函数。

function TrimString($String, $Length)
{
if(strlen($String) > $Length)
{
$Temp[0] = substr($String, 0, $Length);
$Temp[1] = substr($String, $Length);
$SpacePos = strpos($Temp[1], ' ');
if($SpacePos !== FALSE)
{
return $Temp[0].substr($Temp[1], 0, $SpacePos);
}
}
return $String;
}

例子:-

function TrimString($String, $Length){
if(strlen($String)<=$Length){
$stringValue=$String;
} else {
$stringValue=substr($String,0,$Length) . '.';
}
return $stringValue;
}
$m=50;
$a="I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. ";
$data=TrimString($a,$m);
echo $data;

它给了我答案

I am a boy. I am a boy. I am a boy. I am a boy. I .

但我需要答案

I am a boy. I am a boy. I am a boy. I am a boy.

那么你能帮我找到接近提供给该函数的长度的句号并打断段落吗。

最佳答案

在此处添加句号(.)

 return $Temp[0].substr($Temp[1], 0, $SpacePos).".";

更新 2:

<?php
function TrimString($String, $Length){
if(strlen($String)<=$Length){
$stringValue=$String;
} else {
$new_l = strpos ($String , ".", $Length);
$Length = $new_l+1;
$stringValue=substr($String,0,$Length);
}
return $stringValue;
}
$m=50;
$a="I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. I am a boy. ";
$data=TrimString($a,$m);
echo $data;
?>

输出:

I am a boy. I am a boy. I am a boy. I am a boy. I am a boy.

关于接近 100 个单词的句号后分页的 Php 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25382027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com