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

Split a String into Two by Character Length with PHP

If you're in the business of printing URLs to a screen, you'll often have a need to split the string into two separated by a string of three dots (...). The link title tag is often used to show the entire URL. There are two functions on this page (formerly published on Internoetics) that accomplish the same thing; the first is easier to use.

The function will take a long string (say, something like http://www.beliefmedia.com/some-directory/another-directory/this-is-a-long-string-we-want-to-truncate) and return a split string: http://www.beliefmedia.com/som...nt-to-truncate. While I've used a URL for the example, any string can be used.

substr-replace

The first function utilises PHP's substr-replace function.

1
<?php 
2
/*
3
 Split a String into Two by Character Length with PHP
4
 http://www.beliefmedia.com/php-split-string
5
*/
6
 
7
function beliefmedia_split_string($string, $maxcharacters = '62') {
8
  $string_length = strlen($string);
9
 return ($string_length > $maxcharacters) ? substr_replace($string, '...', $maxcharacters/2, $string_length - $maxcharacters) : $string;
10
}
11
 
12
/* Usage */
13
$string = "The Quick Brown Fox Jumped Over the Lazy Dog";
14
echo beliefmedia_split_string($string, $maxcharacters = '30');

The output of the above code would be The Quick Brown...er the Lazy Dog (bolding is mine). Note that the $maxcharacters doesn't include the 3 dots (or whatever separator you might replace it with). In our case, replacing the centre of the string with three dots means the total string length will be 33 characters.

preg_replace

The second function uses a preg_replace_callback() to truncate a URL into a split URL link.

1
<?php 
2
/*
3
 Split a String into Two by Character Length with PHP
4
 http://www.beliefmedia.com/php-split-string
5
*/
6
 
7
function beliefmedia_split_url($matches) {
8
  $url = $matches[2];
9
  $short_url = preg_replace('~^([^/]*)/(.{5})(.{3,})(.{5})$~', '$1/$2...$4', $url);
10
 return $matches[1] . '<a href=&quot;http://' . $url . '&quot;>http://' . $short_url . '</a>' . $matches[3];
11
}
12
 
13
/* Usage */
14
$text = 'http://www.beliefmedia.com/some-long-url-to-be-split';
15
$text = preg_replace_callback("/(\s)http:\/\/([\S]+?)(\s)/i", 'beliefmedia_split_url', "$text");
16
echo $text;

The function will split the the portion of the URL string that comes after the root domain name (the domain name is preserved).

Download


Title: Split a String into Two with PHP
Description: Split a String (or URL) into two by character length with PHP. Includes two functions.
  Download • Version 0.2, 656.0B, zip, Category: PHP Code & Snippets

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

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433