2016-09-26 28 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