note 120286 added to function.explode

From: Date: Wed, 07 Dec 2016 11:39:04 +0000
Subject: note 120286 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-208252@lists.php.net to get a copy of this message
A simple usage for the explode function to get an excerpt from a text. <?php /** * @param int $maxWords * @param string $ellipsis * * @return string */ function getExcerpt($text, $maxWords = 16, $ellipsis = '...') { $wordsCut = $maxWords-1; $words = explode(' ', $text, $wordsCut); if(count($words) < $wordsCut) { return $text; } array_pop($words); return implode(' ', $words) . $ellipsis; } $str = 'Hello World, i am fine!'; var_dump(getExcerpt($str, 4); // Outputs: Hello World, i am... ?> ---- Server IP: 72.52.91.14 Probable Submitter: 77.181.104.130 ---- Manual Page -- http://php.net/manual/en/function.explode.php Edit -- https://master.php.net/note/edit/120286 Del: integrated -- https://master.php.net/note/delete/120286/integrated Del: useless -- https://master.php.net/note/delete/120286/useless Del: bad code -- https://master.php.net/note/delete/120286/bad+code Del: spam -- https://master.php.net/note/delete/120286/spam Del: non-english -- https://master.php.net/note/delete/120286/non-english Del: in docs -- https://master.php.net/note/delete/120286/in+docs Del: other reasons-- https://master.php.net/note/delete/120286 Reject -- https://master.php.net/note/reject/120286 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#208252) next »