0
我有一个字符串数组,例如:字符串数组需要格式化
$testArray = array ('here I am going to have a string that is over 50 charactersss ', 'where I am going to hav string that is over 50 charactersss');
我需要基本上创建其中将采取阵列或多个阵列和计数字符串的长度的函数。如果是超过30个字符则它将执行该操作是从数组中删除的项目,并创建新的线,其不超过30个字符,词语是完好这样的字符串:
'here I am going to have a
string that is over 50 where
I am going to have a string
that is over 50 charactersss'
到目前为止,我已经玩了几天,但不能得到这个工作...
这是我已经尝试我需要一种计数方式,如果数组中的字符串长度超过30,然后我可以应用像这样:
public function textFunction($content = [])
{
$rawText = implode(' ', $content);
$cleanText = trim(preg_replace('/\s\s+/', ' ',
str_replace("\n", " ", $rawText)));
$formattedText = wordwrap($cleanText, 30, "\n");
return $formattedText;
}
以上的工作,但我该如何检查如果数组中的内容超过30个字符来执行此操作,我将不得不循环内容,但如果这样做有意义,则无法执行上述操作。
这就是问题的一个很好的说明,但什么是真正重要的这里是展示你的尝试。即使是一个疯狂的误导尝试总比没有好:它表明你致力于解决问题。 – tadman
使用[wordwrap](http://php.net/manual/en/function.wordwrap.php)。 – FirstOne
这是我试过的,我需要一种计数方式,如果在数组中的字符串长度超过30,那么我可以应用这样的事情: – gold82