字符串分节,避免把一个句子分在两节。(用于百度翻译api)

之所以写这个函数是因为调用百度翻译API的时候,一次性传入的文章太长了,因此需要分节,按块大小分节,比如8K一节。可分节的时候,有可能把一句话拆在2节里,因此,写这个函数来避免这种 情况。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 </head>

 <body>
 <?php

  //这里的文本是经过预处理的,提前把句子或者段落分好。方法很多,我是利用句号,结合括弧啥的。反正找规律
  $str="member steps down EXTRA
Tuesday, September 22, 2020 7:21 AM ET
By  Rhema Peñaflor
Market Intelligence
S&P Global Market Intelligence presents a daily roundup of management and board changes at financial institutions in Europe.
 This feature excludes C-level leadership changes and previously reported stories.


Vontobel

Swiss investment manager Vontobel Holding AG appointed Toby Triebel to take over the management of its digital investing client unit and become a member of its operating committee, effective Oct.
 5.
 The unit is currently headed by CEO Zeno Staub.


Triebel joins from Wealthsimple US Ltd., where he serves as CEO Europe and head of international.


Close Brothers Group

U.K.-based Close Brothers Group PLC senior independent nonexecutive director Geoffrey Howe will step down from the board of directors after the company's annual general meeting in November.


The board appointed Oliver Corbett, chair of the audit committee and independent nonexecutive director, interim senior independent director until Howe's permanent successor is named.


Links are current as of publication time, and we are not responsible if those links are unavailable later.





This article was published by S&P Global Market Intelligence and not by S&P Global Ratings, which is a separately managed division of S&P Global.
";


function fun_split_str($str,$cs=600){ //$cs=600;//段落数据的大小
//这个函数用来分割段落,输入的字符串要求之前预处理,进行分句。句子之间使用 \r\r进行分割
//函数就根据\r来判断,避免把一个句子分成两节。
  $arr1=str_split($str,$cs);
  for($i=0;$i<count($arr1);$i++){
	$ipos=strrpos($arr1[$i],"\r");//注意:这里是取最后一次出现回车的位置
	$arr2[$i]=substr($arr1[$i],0,$ipos);//a2保留回车之前的部分
	$arr3[$i]=substr($arr1[$i],$ipos);//a3存放被截掉的字符串,回车之后的部分
  }
 
 // "移位数据(基于上一节的结果)";
	$j=0;
	for($i=0;$i<count($arr1);$i++){
		$ipos=strrpos($arr1[$i],"\r");//注意:这里是取最后一次出现回车的位置
		//echo $ipos;
		if($i>0){
			$arr2[$i]= $arr3[$i-1].$arr2[$i];//将a3的数值,拼接在下一行a2的前面
		} 
		/*echo $arr2[$i];
		echo "<br>";echo "<br>";
		echo "<hr size=10px>";*/
		$j=$i;
	}
	//执行到这里 i=3,j=2
	//$arr2[$i]=$arr3[$j];
	//echo $arr2[$i];
	return $arr2;
}//end of fun

	echo str_replace("\r","<br>", $str);
	echo "<hr>";

	print_r( fun_split_str($str));

  ?>
 </body>
</html>

执行结果:
Array (
[0] => member steps down EXTRA Tuesday, September 22, 2020 7:21 AM ET By Rhema Peñaflor Market Intelligence S&P Global Market Intelligence presents a daily roundup of management and board changes at financial institutions in Europe. This feature excludes C-level leadership changes and previously reported stories. Vontobel Swiss investment manager Vontobel Holding AG appointed Toby Triebel to take over the management of its digital investing client unit and become a member of its operating committee, effective Oct. 5. The unit is currently headed by CEO Zeno Staub.
[1] => Triebel joins from Wealthsimple US Ltd., where he serves as CEO Europe and head of international. Close Brothers Group U.K.-based Close Brothers Group PLC senior independent nonexecutive director Geoffrey Howe will step down from the board of directors after the company’s annual general meeting in November. The board appointed Oliver Corbett, chair of the audit committee and independent nonexecutive director, interim senior independent director until Howe’s permanent successor is named.
[2] => Links are current as of publication time, and we are not responsible if those links are unavailable later. This article was published by S&P Global Market Intelligence and not by S&P Global Ratings, which is a separately managed division of S&P Global.
[3] => )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欧色

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值