Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Example Interest Rates: Home Loan Variable: 1.89% (1.9%*) • Home Loan Fixed: 1.84% (2.11%*) • Fixed: 1.84% (2.11%*) • Variable: 1.89% (1.9%*) • Investment IO: 2.05% (4.36%*) • Investment PI: 1.58% (2.72%*)

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.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment