Last active
July 13, 2016 10:15
-
-
Save pistou/75ca919e8c9f32e3c4c56914c3a97613 to your computer and use it in GitHub Desktop.
Get N first characters of a string, without cutting off the last word
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 | |
$nbChars = 50; | |
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in ullamcorper est. Quisque semper commodo maximus. Pellentesque pharetra libero lorem, at scelerisque eros sollicitudin ac. Phasellus ullamcorper sem nisi, in tristique augue tincidunt eu. Vivamus congue at felis at porttitor. Curabitur eget urna ac arcu pharetra blandit. Etiam non facilisis ante. Proin iaculis consequat leo sit amet faucibus. Nulla sed venenatis nisl, eget placerat dolor. Donec aliquam egestas dui vitae faucibus. Quisque eget suscipit mauris. Vivamus eget sem erat."; | |
echo abstract($text, $nbChars); | |
function abstract(string $text="", int $nbChars=50) { | |
$abstract = explode("\n", wordwrap($text, $nbChars)); | |
return $abstract[0] . " ..."; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment