Gags
Gags

Reputation: 3829

Smarter string break in PHP string

I wanted to break a string into max 35 character but with intelligent break. Suppose i have string below:

street name, sector name, city, state, pincode

or

ABC Heights, Golf Road, City Test, City Sector

Now, i want these strings to be break in anyway but it shall be intelligent not to break any wrong line.

Eg. break can be

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

NOT

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

I have tried answers here

SMart Wrap

but no luck.

Upvotes: 0

Views: 70

Answers (1)

Carl
Carl

Reputation: 1816

Something like?

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

Which would result in

ABC Heights, Golf Road, City Test,
City Sector

See http://php.net/manual/en/function.wordwrap.php for more details.

Upvotes: 1

Related Questions