Last active
March 7, 2017 20:25
-
-
Save PauliusKrutkis/15e443ae398d5d18b99472c14dec644c to your computer and use it in GitHub Desktop.
PHP split text into 2 parts based on length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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