xyn Posted June 30, 2007 Share Posted June 30, 2007 Hey. From my previous post i've came with a small solution. however that was to display the tutorials pages by amount of characters.... i have made a small function. function tut2pages($text, $pagenumber, $setpage = false) { $totalchars = 5; if($setpage === FALSE) { $pages = floor(strlen($text) / $totalchars); } else { if(!$pagenumber) { $pagenumber = 0; } $pages = substr($text, $totalchars, -$pagenumber); } return $pages; } The problem is, in my text file. i have: 'abcde abcde abcde abcde abcde' Now when it goes through my function i wanted it to display abcde. however it does the opposite... and displays 'abcde abcde abcde abcde' removing 1 of the 'abcde' terms :S any ideas? Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/ Share on other sites More sharing options...
Barand Posted June 30, 2007 Share Posted June 30, 2007 :S any ideas? None that http://www.php.net/substr won't tell you. And you need ceil(), not floor(). If there are 1500 chars at 1000 per page then there are 2 pages required Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-286897 Share on other sites More sharing options...
corbin Posted June 30, 2007 Share Posted June 30, 2007 If you wanted to get the 2nd through 5th character in a string you would do: substr(1, 4); That would start at position 1 (they're enumerated from 0), and it would end at position 4. I don't know why you're throwing in the third parameter, since technically you don't need it if you calculate the first two correctly... I guess you're approach does the same thing in a way, but.... Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-286900 Share on other sites More sharing options...
Barand Posted June 30, 2007 Share Posted June 30, 2007 try something like <?php function tut2pages($text, $page) { $pagesize = 5; return substr($text, ($page - 1)*$pagesize, $pagesize); } $tute = 'abcdefghijklmnopqrstuvwxy'; echo tut2pages($tute, 1); // abcde echo tut2pages($tute, 4); // pqrst ?> Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-286911 Share on other sites More sharing options...
xyn Posted July 1, 2007 Author Share Posted July 1, 2007 try something like <?php function tut2pages($text, $page) { $pagesize = 5; return substr($text, ($page - 1)*$pagesize, $pagesize); } $tute = 'abcdefghijklmnopqrstuvwxy'; echo tut2pages($tute, 1); // abcde echo tut2pages($tute, 4); // pqrst ?> Thanks. thats excellent. My final question is... Say i set my tutorials by paginating every 1000 characters... How do i calculate the Pages? for pagination? tutorials with 500 chars, will only have 1 page... but something for 3400 chars will need 4 pages. and i just don't know how i would calculate the amount of pages required per tutorial, and set-up the pagination.. Any help would be deeply appreciated? thanks for the help so far! Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-287348 Share on other sites More sharing options...
Barand Posted July 1, 2007 Share Posted July 1, 2007 As I stated in earlier post, use ceil() $pages = ceil ($total_chars / $chars_per_page); Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-287354 Share on other sites More sharing options...
xyn Posted July 1, 2007 Author Share Posted July 1, 2007 ok. thanks. Link to comment https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-287355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.