A continues series of characters in a string how do I split it at a certain point e.g 30 charaacters in php

只愿长相守 提交于 2020-01-16 16:25:09

问题


I'm trying to break/split a continues series of characters in a php string at a specific point e.g.

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

to

AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA

回答1:


PHP has a function called chunk_split() which does exactly what you ask...

$myLongString = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';

$splitString = chunk_split($myLongString, 20);

echo $splitString;

/*

AAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAA

*/



回答2:


I would use wordwrap() with the $break parameter as " ".




回答3:


Just take a look at substr function here..

eg:

substr('foobar', 0, 3). ' ';

yields

'foo '


来源:https://stackoverflow.com/questions/4348892/a-continues-series-of-characters-in-a-string-how-do-i-split-it-at-a-certain-poin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!