Jump to content

[SOLVED] substr help!


xyn

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.