Jump to content

String length stuff not making up sense?


tallberg

Recommended Posts

I'm getting a weird result from the following code. Thanks if any one can help.

Result:

[quote]start: 0 end: 5000 strlen: 5000

start: 5000 end: 10000 strlen: 8440

start: 10000 end: 15000 strlen: 3440
[/quote]

I would of thought the strlen could only be 5000 as the start and end of the substr function are incrementing by 5000.

[code]$start = 0;
$end = 5000;

for($i=1; $i <= $pages; $i++)
{

$p_Tempdescription = substr($p_description,$start,$end);
 
echo "<br>";
  echo "start: " . $start . " end: ".  $end . " strlen: " . strlen($p_Tempdescription);
echo "<br>";

$start += 5000;
$end += 5000;


}[/code]
Link to comment
https://forums.phpfreaks.com/topic/35532-string-length-stuff-not-making-up-sense/
Share on other sites

The third parameter of substr is not end, but length.
So you're saying
$p_Tempdescription = substr($p_description,$start,$end);
$p_Tempdescription = substr($p_description,0,5000);
$p_Tempdescription = substr($p_description,5000,10000); <- sub string will be 10,000 chars, not 5000.

Keep your third param 5000 each time.

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.