Hi All,
I have a string of 1500 or more characters. but I want to go through this string and split it at the 160th but it should not split in the middle of the word. If the 160th character is not the end of a word it should go back to the next word and split from that. My code is below but its not working well
I have a string of 1500 or more characters. but I want to go through this string and split it at the 160th but it should not split in the middle of the word. If the 160th character is not the end of a word it should go back to the next word and split from that. My code is below but its not working well
Code:
sub get_horoscope { my($dbh,$thetype, $hname, $phone)=@_; my($val,$sth,$rec,$horoscope,$subset,$i); $sth=$dbh->prepare("SELECT ? FROM horoscope_table WHERE `horoscope_name` = '$hname'"); my($more)=$dbh->prepare("INSERT INTO chat_outbox (`sender`, `phone`,`text`,`insertdate`) VALUES ('466',TRIM(?),TRIM(?),NOW())"); $sth->execute($thetype); $horoscope=""; my(@types); my($start, $end) = (0, 160); my $set = length($rec->{'$thetype'}) % 160; while($rec=$sth->fetchrow_hashref()) { if(length($rec->{'$thetype'}) > 160){ for($i = 0; $i <= $set; $i++){ $subset = substr($rec->{'$thetype'}, $start, $end); $start+=160; $end+=160; $more->execute($subset); }else{ $horoscope = "$horoscope\n$rec->{'$thetype'}"; } } } $sth->finish(); syslog('info',__LINE__."::[get_horoscope] $horoscope"); return ($horoscope); }
Comment