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
https://forums.phpfreaks.com/topic/57884-solved-substr-help/
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
https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-286900
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   

?>

Link to comment
https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-286911
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
https://forums.phpfreaks.com/topic/57884-solved-substr-help/#findComment-287348
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.