note 85994 added to function.substr
| From: | Elliot at notwothesame dot com | Date: | Sat, 27 Sep 2008 16:44:01 +0000 |
| Subject: | note 85994 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144841@lists.php.net to get a copy of this message | ||
Your function doesn't quite work as expected. If you want to insert something, say 20
characters from the end it works, but not 20 characters from the beginning.
Slight tweak:
function insert_substr($str, $pos, $substr) {
$pos = strlen($str) - $pos;
$part1 = substr($str, 0, -$pos);
$part2 = substr($str, -$pos);
return $part1.$substr.$part2;
}
$str = "12345678901234567890123456789";
echo insert_substr($str, 20, "<br />");
Output: "12345678901234567890<br />123456789"
----
Server IP: 92.48.74.199
Probable Submitter: 82.2.56.49
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/85994
Del: integrated -- https://master.php.net/note/delete/85994/integrated
Del: useless -- https://master.php.net/note/delete/85994/useless
Del: bad code -- https://master.php.net/note/delete/85994/bad+code
Del: spam -- https://master.php.net/note/delete/85994/spam
Del: non-english -- https://master.php.net/note/delete/85994/non-english
Del: in docs -- https://master.php.net/note/delete/85994/in+docs
Del: other reasons-- https://master.php.net/note/delete/85994
Reject -- https://master.php.net/note/reject/85994
Search -- https://master.php.net/manage/user-notes.php