Try something like this
function cutText($inputText, $start, $length) { $temp = $inputText; $res = array(); while (strpos($temp, '>')) { $ts = strpos($temp, '<'); $te = strpos($temp, '>'); if ($ts > 0) $res[] = substr($temp, 0, $ts); $res[] = substr($temp, $ts, $te - $ts + 1); $temp = substr($temp, $te + 1, strlen($temp) - $te); } if ($temp != '') $res[] = $temp; $pointer = 0; $end = $start + $length - 1; foreach ($res as &$part) { if (substr($part, 0, 1) != '<') { $l = strlen($part); $p1 = $pointer; $p2 = $pointer + $l - 1; $partx = ""; if ($start <= $p1 && $end >= $p2) $partx = ""; else { if ($start > $p1 && $start <= $p2) $partx .= substr($part, 0, $start-$pointer); if ($end >= $p1 && $end < $p2) $partx .= substr($part, $end-$pointer+1, $l-$end+$pointer); if ($partx == "") $partx = $part; } $part = $partx; $pointer += $l; } } return join('', $res); }
Parameters:
- $ inputText - text input
- $ start - position of the first character
- $ length - as the menu characters we want to remove
Example # 1 - Deleting the first three characters
$text = 'Test <span><a>something</a> something else</span>.'; $text = cutText($text, 0, 3); var_dump($text);
Exit (Deleted "Tes")
string(47) "t <span><a>something</a> something else</span>."
Delete the first 10 characters
$text = cutText($text, 0, 10);
Exit (removed "Test somet")
string(40) "<span><a>hing</a> something else</span>."
Example 2 - Removing internal characters - "es" from "Test"
$text = cutText($text, 1, 2);
Exit
string(48) "Tt <span><a>something</a> something else</span>."
Removing "thing something el"
$text = cutText($text, 9, 18);
Exit
string(32) "Test <span><a>some</a>se</span>."
Hope this helps.
Well, maybe this is not the best solution, but all that I can do at the moment.