note 116964 deleted from function.substr by tularis

From: Date: Sun, 29 Mar 2015 20:35:44 +0000
Subject: note 116964 deleted from function.substr by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-202071@lists.php.net to get a copy of this message
Note Submitter: chris ---- When using substr you need to remember that the second value is the number of characters to return so when using in conjunction with strpos to return a mid section of a string you need to double up. In my case, string containing the current file with webpath. <?php $page = "/files/filename.php"; // this doesn't work, returns everything because position 16 the period is longer than the remaining substring. substr($page, 7, strpos($page, '.')); // This does work, here is my approach with little portability added to it. // add optional value of 1 to skip the first leading slash and obtain the next string position. $slash = strpos($page, "/", 1) +1; // everything after the slash $page_slash = substr($page, $slash); // now we have a short string and can $page_name = substr($page_slash, 0, strpos($page_slash, '.')); // will return filename ?>

« previous php.notes (#202071) next »