note 116964 added to function.substr

From: Date: Fri, 27 Mar 2015 19:38:02 +0000
Subject: note 116964 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-202055@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 72.52.91.14 Probable Submitter: 80.229.222.99 ---- Manual Page -- http://php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/116964 Del: integrated -- https://master.php.net/note/delete/116964/integrated Del: useless -- https://master.php.net/note/delete/116964/useless Del: bad code -- https://master.php.net/note/delete/116964/bad+code Del: spam -- https://master.php.net/note/delete/116964/spam Del: non-english -- https://master.php.net/note/delete/116964/non-english Del: in docs -- https://master.php.net/note/delete/116964/in+docs Del: other reasons-- https://master.php.net/note/delete/116964 Reject -- https://master.php.net/note/reject/116964 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202055) next »