views:

137

answers:

3
$string = "This is my page content. This text will be paginated.";
$pageNo = "0";
$pieceLength = "12";  

$preparedForPrint = substr($string,$pageNo,$pieceLength);

what i want to do is if the 12th character is inside a word(the 12th character is not a space) i want to move my cursor 'till it finds a space an return that substring despite the fact that is more than 12 characters long. how can i do that? thanks

+2  A: 

something like this:

while ($string[$pieceLength]!=' ' || $string[$pieceLength]!='\n')
   $pieceLength++;

substr($string, $pageNo, $pieceLength);

Also consider php builtin wordwrap

Aif
A: 

Look at strpos(); offset would be $pieceLength, but position returned is still from beginning of the haystack.

JP
+4  A: 

You can use strpos()

$pieceLength = strpos($string," ",12);

http://php.net/manual/en/function.strpos.php

Tim