Jump to content

Help with separating a text string.


Parobola

Recommended Posts

Hello again, I have a problem I need to overcome and cannot fathom how.

 

I need to take a random length character string i.e. "This is a sample of what I need to accomplish."

 

Then I need to separate the character string into lengths that won't push past the boundaries of a FPDF cell.  This is easy to do using strlen( ); but if I seperate by string length then I will end up with part of a word one line and part of another word on another line. 

 

So what I thought I would do is separate the words using explode(" " , $string); and dump it into an array.  Then pull out each word, add it to a string, and use strlen( ); to check my length and use IF (strlen($string) >= 10) {start a new line} ELSE {add another word from array and restart test}.

 

My issue is that I can't figure out how to use the explode function with the array function properly.  Any advice on where I can look, or anyone want to give me an example of how I could accomplish this?

Link to comment
Share on other sites

$string = "this is a sample string for learning";
$exp = explode(" ",$string);
foreach($exp as $value){
   if(strlen($value) >= 10){
       //do something
   }else{
       // do something else
   }
}

 

since I do not fully understand what exactly you are wanting to do to the strings if they are under 10 chars and above 10 chars, I can't quite finish this code...if you need further help let me know.

 

Edit: wordwrap could be what you want here yes.. :thumb-up:

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.