note 86254 added to function.substr-replace

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
The preemptive test to see if $string is "too long" shouldn't add strlen($replacement) to $max.  $max should represent the absolute maximum length of string returned.  The size of the $replacement is irrelevant in that determination.

The rest of the function (unchanged below) operates as defined above.  Meaning, the size of the $replacement is subtracted from the $max, so that the returned string is exactly the length of $max.

<?php
function truncate($string, $max = 20, $replacement = '')
{
    if (strlen($string) <= $max)
    {
        return $string;
    }
    $leave = $max - strlen ($replacement);
    return substr_replace($string, $replacement, $leave);
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 69.223.24.72
----
Manual Page -- http://www.php.net/manual/en/function.substr-replace.php
Edit        -- https://master.php.net/note/edit/86254
Del: integrated  -- https://master.php.net/note/delete/86254/integrated
Del: useless     -- https://master.php.net/note/delete/86254/useless
Del: bad code    -- https://master.php.net/note/delete/86254/bad+code
Del: spam        -- https://master.php.net/note/delete/86254/spam
Del: non-english -- https://master.php.net/note/delete/86254/non-english
Del: in docs     -- https://master.php.net/note/delete/86254/in+docs
Del: other reasons-- https://master.php.net/note/delete/86254
Reject      -- https://master.php.net/note/reject/86254
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.