Jump to content

How to split a string cleanly into at most 2 strings


hadoob024

Recommended Posts

I almost have this working but am missing something.  I have a string that I need to split into at most 2 smaller strings.  It has to be split cleanly (not in the middle of text), and the first string cannot be longer than 25 characters.  So for example, the following:

 

$mystring = "This Is My Manufacturer Name That Can Be Long"

 

 

Should be stored into 2 smaller strings like:

 

$mystring1 = "This Is My Manufacturer"
$mystring2 = "Name That Can Be Long"

 

 

I was going to use wordwrap() but that won't work because I need the values to be stored in 2 separate string variables.  Is using explode() the way to go?

 

 

Btw, this is what I have.  Is this the best way to do it?

 

                    
$mfg_name_length = strlen($manu->name);
if ($mfg_name_length >= 25) {
     $mfg_name_array = explode(" ", $manu->name);
     $space_loc = strrpos(substr($manu->name, 0, 25), " ");
     $pdf->addText(45,675,14,"Manufacturer:    ".substr($manu->name, 0, $space_loc));
     $pdf->addText(45,660,14,"Manufacturer:    ".substr($manu->name, $space_loc+1, $mfg_name_length));

Link to comment
Share on other sites

Cool.  Thanks.  Yeah.  I ended up using that.  I think my concern was splitting the string cleanly so that I'm not cutting it in the middle of the string.  I used strrpos() to get the last instance of a space (within the first 25 characters of the string).  Then I store the value of the string from the beginning until the value returned by strrpos().  Then I store the value from strrpos()+1 to the length of the string in the second variable.  It seems to work.  Looking for ways that it might fail though.

Link to comment
Share on other sites

Doesn't wordwrap() return just 1 string containing the original string with the breaks in it ready to display?  I need to split my original string up into 2 strings because I'm displaying them using ezPDF creator's addText() method.  Or maybe I'm misunderstanding the usage of wordwrap()? 

Link to comment
Share on other sites

<?php
$mystring = "This Is My Manufacturer Name That Can Be Long";

$strs = explode("\n", wordwrap($mystring, 25, "\n"));

echo "1: " . $strs[0] .  "\n2: " . $strs[1];

 

Returns:

1: This Is My Manufacturer
2: Name That Can Be Long

 

Which, I believe, met your criteria.

Link to comment
Share on other sites

Is the string even going to have a \n on its own?

 

In that case you get 3 strings, or 2+however many "\n"s there are in the string, right?

 

You could, but I'm guessing it would be unlikely with the info provided. Changing the delimiter would easily fix this.

$strs = explode("|||", wordwrap($mystring, 25, "|||"));

Link to comment
Share on other sites

Is the string even going to have a \n on its own?

 

In that case you get 3 strings, or 2+however many "\n"s there are in the string, right?

 

 

Not sure exactly.  The problem is that they shouldn't have any, and there's no limit to how many there might be, or what other bad data might be contained in addition to these extraneous newlines and carriage returns.  Bad data was entered into the system, and that's what I'm responsible for cleaning up on displaying it.  There shouldn't be any carriage returns or newlines, but no one screened data entry  ::)

Link to comment
Share on other sites

So then you might want to use a character other than \n in the wordwrap/explode calls, like KingPhilip suggested. Even Something super weird, like a password. {}++_FGShc@, you know?

Link to comment
Share on other sites

Cool.  Thanks a ton for the tips!  Yeah, I queried the db and it seems like the only bad characters right now are extraneous newlines and carriage returns.  Instead of editing those records they just want me to "make it work" for display purposes.  ::)

 

And I think they're putting something in place to clean/format the data entry for the future.  Hopefully.  ;)

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.