2016-09-26 27 views
0

我想将字符串分解为max 35 character,但是智能中断。假设我有以下字符串:PHP字符串中的智能字符串中断

street name, sector name, city, state, pincode 

ABC Heights, Golf Road, City Test, City Sector 

现在,我想这些字符串无论如何都要打破,但须智能没有打破任何错行。

例如,休息可以

ABC Heights, Golf Road,<br/> 
City Test, City Sector 

这里

SMart Wrap

,但没有运气不

ABC Heights, Golf Road,City Te<br/> 
st, City Sector 

我已经试过的答案。

+0

[wordwrap](http://php.net/manual/en/function.wordwrap.php)+ [nl2br](http://php.net/nl2br) –

回答

1

有点像?

<?php 
    $string = "ABC Heights, Golf Road, City Test, City Sector"; 
    echo wordwrap($string, 35, "<br />\n"); 
?> 

这将导致

ABC Heights, Golf Road, City Test, 
City Sector 

详情请参阅http://php.net/manual/en/function.wordwrap.php

+0

我可以保存这个损坏的字符串到2 diff变量 – Gags

+0

是的,只是'爆炸'在分隔符 - 在这种情况下'$ lines = explode(“
\ n”,$ wordwrap_string);'然后设置$ lines [0]/[1],无论你想要他们。 – Carl