Skip to content

Instantly share code, notes, and snippets.

@PauliusKrutkis
Last active March 7, 2017 20:25
Show Gist options
  • Save PauliusKrutkis/15e443ae398d5d18b99472c14dec644c to your computer and use it in GitHub Desktop.
Save PauliusKrutkis/15e443ae398d5d18b99472c14dec644c to your computer and use it in GitHub Desktop.
PHP split text into 2 parts based on length
<?php
function truncate($full, $length)
{
$length = abs((int)$length);
if (strlen($full) > $length) {
$first = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1', $full);
$second = substr($full, strlen($first));
}
return [
'first' => $first,
'second' => $second
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment