按字符数将MySQL字段拆分为最接近的完整字?

时间:2016-11-27 18:29:46

标签: php mysql sql string split

我有一个用于降价格式的博客帖子正文的MySQL字段。由于我使用的API,我只能发送3000个字符块,但是我的一些帖子大到4500个字符,并且有超过2000个字符,所以我不想手动拆分它们。

我正在尝试找出一个函数来检查列中每个字段的char_length,如果它超过3000个字符,该函数会将超过3000个字符(四舍五入到最近的单词)的任何内容拆分成第二列有。它超出了我之前处理的功能范围,所以我希望能够朝着正确的方向发展。这是我到目前为止的基础:

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上中断,例如将<div>拆分为<div" ">,如果这是第3000个字符。

对于背景,我已经阅读了以下内容:

Split string into table in groups of 26 characters or less, rounded to the nearest word

响应似乎已经提出了自定义函数来根据设定的长度拆分字符串,虽然函数没有得到很好的解释/评论,所以我有点迷失。

如果在MySQL中做起来不容易,我愿意在PHP中将其拉出来并在那里操纵数据。

任何见解都将不胜感激!

2 个答案:

答案 0 :(得分:2)

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
;       
+---------------------------+------------------------------------------+
| field_1                   | field_2                                  |
+---------------------------+------------------------------------------+
| My name is Inigo Montoya! | You've killed my father, prepare to die! |
+---------------------------+------------------------------------------+

完整示例

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        |
+------------------------------+---------------------------+

答案 1 :(得分:1)

该代码将始终将字符串除以3000个字符并将其推送到数组。无论字符长度是多少,您都可以使用此代码块。不要忘记,如果你的文字的字符低于3000,那么$ bodyParts变量中只有1个元素。

$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);