note 44698 added to function.substr

From: Date: Fri, 13 Aug 2004 16:18:05 +0000
Subject: note 44698 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-79982@lists.php.net to get a copy of this message
I believe this sample code is useful for many like me who have trouble remembering how substr works with negative numbers. <?php $input = "abcdefg"; echo substr($input, 0, 2), "\n"; /* Outputs "ab" */ echo substr($input, 0, -2), "\n"; /* Outputs "abcde" */ echo substr($input, 2), "\n"; /* Outputs "cdefg" */ echo substr($input, -2), "\n"; /* Outputs "fg" */ echo substr($input, 2, 1), "\n"; /* Outputs "c" */ echo substr($input, -2, 1), "\n"; /* Outputs "f" */ echo "'", $input[2], "'\n"; /* Outputs "'c'" */ echo "'", $input[-2], "'\n"; /* Outputs "''" */ ?> (Ps: The preview function seems to eat my backslashes. Hope the actual posting doesn't. In case it did anyway, imagine that every "n" has a backslash.) ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44698 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44698&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44698&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#79982) next »