There are two ways to do this:
(1) uses html_entity_decode() and htmlentities() :
$s = html_entity_decode($mytext); $sub = substr($s, 0, 6); echo htmlentities($sub);
(2) might look something like this:
if (preg_match('!^([^&]|&(?:.*?;)){0,5}!s', $mytext, $match)) { echo $match[0]; }
What it is: find up to 5 occurrences of the previous expression from the beginning of the line. Previous expression:
any character that is not an ampersand; or
an ampersand followed by everything, even a semi-colony (including an HTML object).
This is not ideal, so I would prefer (1).
source share