RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Wrap a Long String, Word or URL Over Multiple Lines

A long string of text can often break the formatting of a page - particularly if text is rendered in a narrow table. The solution: Implement a PHP function that will wrap a string over multiple lines.

Line-wrapping is commonly used by utilizing a hyphen (a dash, actually) to divide words at the nearest breakpoint between syllables, indicating that the letters form a word fragment - not a word. In the following case, the breakpoint is defined by the container or table it is in.

If your problem can't be managed by CSS, consider the following.

Wrap a Long String, Word or URL Over Multiple Lines

In this case, the only long 'word' was a URL, and it was split (hyphenated) over multiple lines within a table cell.

1
<?php 
2
/*
3
 Wrap a Long String, Word or URL Over Multiple Lines
4
 http://www.beliefmedia.com/code/php-snippets/soft-break
5
*/
6
 
7
function beliefmedia_soft_break($string, $interval=20, $breakChr='­') {
8
 
9
  $split_string = explode(' ', $string);
10
 
11
  foreach($split_string as $key => $val) {
12
   if(strlen($val)>$interval) {
13
     $split_string[$key] = wordwrap($val, $interval, $breakChr, true);
14
   }
15
  }
16
 return implode(' ', $split_string);
17
}

Usage:

1
<?php 
2
$string = 'Almost a decade after they made their [commercial debut](http://www.boeing.com/news/releases/2001/q2/news_release_010521b.html) on a Boeing ...';
3
echo beliefmedia_soft_break($string);

Depending on your needs, the PHP wordwrap function may be all that you require. If you need to break a URL, consider truncating them.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment