Php 是否按字符数将MySQL字段拆分为最接近的完整单词?
我有一个MySQL字段,用于标记格式的博客文章的正文。由于我使用的API,我只能发送3000个字符的块,但是我的一些帖子多达4500个字符,其中有2000多个字符,所以我不想手动拆分它们 我试图找出一个函数来检查列中每个字段的字符长度,如果超过3000个字符,该函数将把超过3000个字符(四舍五入到最接近的单词)的内容拆分为第二列。这超出了我以前处理过的功能范围,因此我希望朝着正确的方向推进。以下是我到目前为止所做工作的基础:Php 是否按字符数将MySQL字段拆分为最接近的完整单词?,php,mysql,sql,string,split,Php,Mysql,Sql,String,Split,我有一个MySQL字段,用于标记格式的博客文章的正文。由于我使用的API,我只能发送3000个字符的块,但是我的一些帖子多达4500个字符,其中有2000多个字符,所以我不想手动拆分它们 我试图找出一个函数来检查列中每个字段的字符长度,如果超过3000个字符,该函数将把超过3000个字符(四舍五入到最接近的单词)的内容拆分为第二列。这超出了我以前处理过的功能范围,因此我希望朝着正确的方向推进。以下是我到目前为止所做工作的基础: SELECT `Body` from `blogposts` WHE
SELECT `Body` from `blogposts`
WHERE char_length(Body) > 3000
SET
Body2 = SUBSTRING(`Body`, 3001, char_length(Body))
Body = SUBSTRING(`Body`, 1, 3000)
由于它还没有完成,我还没有测试它。我不相信它会接近我想要的,但在测试之前,我仍在努力解决另外两个问题:
1) 如何将其放在最近一个单词的末尾(在3000个字符以下四舍五入),而不是正好在第3000个字符处拆分
2) 如果它试图用文字进行处理,它会在文本中的markdown/html上分解,例如将
拆分为
,如果这是第3000个字符
作为背景资料,我已通读了以下内容:
这些响应似乎提供了自定义函数来根据设置的长度分割字符串,尽管这些函数没有得到很好的解释/注释,所以我有点迷茫
如果在MySQL中做起来不容易,我愿意在PHP中处理这些数据
任何见解将不胜感激
update `blogposts`
set `Body2` = substring(`Body`,3000-instr(reverse(left(`Body`,3000)),' ')+1)
,`Body` = left(`Body`,3000-instr(reverse(left(`Body`,3000)),' '))
where char_length(Body) > 3000
;
30个字符的演示
set @Body = 'My name is Inigo Montoya! You''ve killed my father, prepare to die!';
select left(@Body,30-instr(reverse(left(@Body,30)),' ')) as field_1
,substring(@Body,30-instr(reverse(left(@Body,30)),' ')+1) as field_2
;
完整示例
该代码总是将字符串除以3000个字符,然后将其推送到数组中。无论字符长度是多少,都可以使用此代码块。不要忘记,如果文本的字符数小于3000,$bodyParts变量中只有一个元素
$bodyText; // That came from SQL Ex Query : SELECT body FROM blogposts
$bodyParts = [];
$lengthOfBody = strlen($bodyText);
if($lengthOfBody > 3000){
$forLoopInt = ceil($lengthOfBody / 3000); // For example if your body text have 3500 characters it will be 2
echo $forLoopInt;
for($i = 0; $i<= $forLoopInt - 2; $i++){
$bodyParts[] = substr($bodyText, ($i) * 3000 , 3000);
}
// lets fetch the last part
$bodyParts[] = substr( $bodyText,($forLoopInt - 1) * 3000);
}else{
$bodyParts[] = $bodyText;
}
/* anyway if your body text have characters lower than 3000 , bodyParts array will contain just 1 element, if not it will have Ceil(Length of body / 3000) elements in it. */
var_dump($bodyParts);
$bodyText;//来自SQLEx查询:从blogposts中选择body
$bodyParts=[];
$lengthOfBody=strlen($bodyText);
如果($lengthOfBody>3000){
$forLoopInt=ceil($lengthOfBody/3000);//例如,如果正文文本有3500个字符,则为2
echo$forLoopInt;
对于($i=0;$iThanks-我正在尝试运行它,但我对这里的子项有点困惑。我想我只需要将任何(at)正文更改为我的列名,但在“字段列表”中出现错误“未知列”正文,所以我想我需要更改集合(at)Body换了别的东西?@AdamS.Cochran,忘了把30换成3000。现在改了吗谢谢!我想我还需要把select换成update来做一个永久性的改变?从测试来看效果不错。@AdamS.Cochran,是的。请备份你的表(创建一个副本)在更新之前。保存这些结果时似乎需要做一些更复杂的事情。因为在更新中使用选择函数似乎不可行?
create table `blogposts` (`Body` varchar(3000),`Body2` varchar(3000));
insert into blogposts (`Body`) values
('Hello darkness, my old friend' )
,('I''ve come to talk with you again' )
,('Because a vision softly creeping' )
,('Left its seeds while I was sleeping' )
,('And the vision that was planted in my brain' )
,('Still remains' )
,('Within the sound of silence' )
,('In restless dreams I walked alone' )
,('Narrow streets of cobblestone' )
,('''Neath the halo of a street lamp' )
,('I turned my collar to the cold and damp' )
,('When my eyes were stabbed by the flash of a neon light' )
,('That split the night' )
,('And touched the sound of silence' )
,('And in the naked light I saw' )
,('Ten thousand people, maybe more' )
,('People talking without speaking' )
,('People hearing without listening' )
,('People writing songs that voices never share' )
,('And no one dared' )
,('Disturb the sound of silence' )
;
select left(`Body`,30-instr(reverse(left(`Body`,30)),' ')) as Body
,substring(`Body`,30-instr(reverse(left(`Body`,30)),' ')+1) as Body2
from `blogposts`
where char_length(Body) > 30
;
+------------------------------+---------------------------+
| Body | Body2 |
+------------------------------+---------------------------+
| I've come to talk with you | again |
+------------------------------+---------------------------+
| Because a vision softly | creeping |
+------------------------------+---------------------------+
| Left its seeds while I was | sleeping |
+------------------------------+---------------------------+
| And the vision that was | planted in my brain |
+------------------------------+---------------------------+
| In restless dreams I walked | alone |
+------------------------------+---------------------------+
| 'Neath the halo of a street | lamp |
+------------------------------+---------------------------+
| I turned my collar to the | cold and damp |
+------------------------------+---------------------------+
| When my eyes were stabbed by | the flash of a neon light |
+------------------------------+---------------------------+
| And touched the sound of | silence |
+------------------------------+---------------------------+
| Ten thousand people, maybe | more |
+------------------------------+---------------------------+
| People talking without | speaking |
+------------------------------+---------------------------+
| People hearing without | listening |
+------------------------------+---------------------------+
| People writing songs that | voices never share |
+------------------------------+---------------------------+
update `blogposts`
set `Body2` = substring(`Body`,30-instr(reverse(left(`Body`,30)),' ')+1)
,`Body` = left(`Body`,30-instr(reverse(left(`Body`,30)),' '))
where char_length(`Body`) > 30
;
select `Body`
,`Body2`
from `blogposts`
where `Body2` is not null
;
+------------------------------+---------------------------+
| Body | Body2 |
+------------------------------+---------------------------+
| I've come to talk with you | again |
+------------------------------+---------------------------+
| Because a vision softly | creeping |
+------------------------------+---------------------------+
| Left its seeds while I was | sleeping |
+------------------------------+---------------------------+
| And the vision that was | planted in my brain |
+------------------------------+---------------------------+
| In restless dreams I walked | alone |
+------------------------------+---------------------------+
| 'Neath the halo of a street | lamp |
+------------------------------+---------------------------+
| I turned my collar to the | cold and damp |
+------------------------------+---------------------------+
| When my eyes were stabbed by | the flash of a neon light |
+------------------------------+---------------------------+
| And touched the sound of | silence |
+------------------------------+---------------------------+
| Ten thousand people, maybe | more |
+------------------------------+---------------------------+
| People talking without | speaking |
+------------------------------+---------------------------+
| People hearing without | listening |
+------------------------------+---------------------------+
| People writing songs that | voices never share |
+------------------------------+---------------------------+
$bodyText; // That came from SQL Ex Query : SELECT body FROM blogposts
$bodyParts = [];
$lengthOfBody = strlen($bodyText);
if($lengthOfBody > 3000){
$forLoopInt = ceil($lengthOfBody / 3000); // For example if your body text have 3500 characters it will be 2
echo $forLoopInt;
for($i = 0; $i<= $forLoopInt - 2; $i++){
$bodyParts[] = substr($bodyText, ($i) * 3000 , 3000);
}
// lets fetch the last part
$bodyParts[] = substr( $bodyText,($forLoopInt - 1) * 3000);
}else{
$bodyParts[] = $bodyText;
}
/* anyway if your body text have characters lower than 3000 , bodyParts array will contain just 1 element, if not it will have Ceil(Length of body / 3000) elements in it. */
var_dump($bodyParts);