slice/paging large contents using php
by Hamid Reza Fahimi Madjd @ Nov 28, 2010
Some days ago i was engaged in wap version of a web based application that had to prepare large content (like articles) for mobile devices.
considering limitation of mobile devices, i decided to slice content to specified bytes (1500 Bytes) and display sliced content instead whole content.
so i wrote a snip code that :
- fetch specified bytes
- continue to fetching (in both side, left and right) untill get first new line character
define('BYTES_PER_PAGE', 1500); define('PAGE_NO', ((int)@$_GET['page']) == 0? 1: (int)@$_GET['page']); $len = mb_strlen($description, 'utf-8'); if($len > BYTES_PER_PAGE) { $len = ($len%BYTES_PER_PAGE == 0? (int)$len/BYTES_PER_PAGE: (int)($len/BYTES_PER_PAGE)+1 ) ; $start = (PAGE_NO - 1) * BYTES_PER_PAGE; $end = PAGE_NO * BYTES_PER_PAGE; // go back till get first new line char while($start > 0 && ord(mb_substr($description, --$start, 1, 'utf-8')) != 10); // go forward till get first new line char while($end < mb_strlen($description, 'utf-8') && ord(mb_substr($description, $end++, 1, 'utf-8')) != 10); $description = mb_substr($description, $start, $end-$start, 'utf-8'); }
as you see, because of utf8 string, i used mb_* functions, you can replace it with normal string functions.
php
recent posts
- › how to use phing to build php projects
- › slice/paging large contents using php
- › how to log methods call in php ?
- › sql IN logical operation for java
- › backup from mysql database's routines
- › how to deploy war file into web root ?
- › how to get all oracle components version ?
- › temporary/memory tables in mysql
- › Set JFreeChart data from database
- › How to search and sort primitive arrays in Java ?
archive
- › 2011/06 (1)
- › 2010/11 (1)
- › 2010/10 (1)
- › 2009/04 (2)
- › 2008/05 (1)
- › 2008/03 (1)
- › 2008/01 (4)
- › 2007/12 (4)
last tweet
- ›