hadoob024 Posted June 8, 2010 Share Posted June 8, 2010 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)); Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/ Share on other sites More sharing options...
dabaR Posted June 8, 2010 Share Posted June 8, 2010 I think http://ca2.php.net/substr is what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069465 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069471 Share on other sites More sharing options...
dabaR Posted June 8, 2010 Share Posted June 8, 2010 Hehe...noone can critique code they don't see :-P (post the code) Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069473 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 My bad. It is in my first post. Forgot to post it originally, so went back and edited my post to include the code. Guess you had already replied prior to me editing my post. Damn you for being too fast Thanks though! Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069477 Share on other sites More sharing options...
Philip Posted June 8, 2010 Share Posted June 8, 2010 Why wouldn't wordwrap work? $strs = explode("\n", wordwrap($str, 25, "\n")) Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069487 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 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()? Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069492 Share on other sites More sharing options...
Philip Posted June 8, 2010 Share Posted June 8, 2010 No, you're understanding wordwrap correctly, but if you use explode on that (as seen in my example) it'll convert it into an array Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069493 Share on other sites More sharing options...
premiso Posted June 8, 2010 Share Posted June 8, 2010 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069496 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 Cool! I had no idea you could do that. Guess you learn something new everyday. Thanks for the tips! Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069503 Share on other sites More sharing options...
dabaR Posted June 8, 2010 Share Posted June 8, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069504 Share on other sites More sharing options...
Philip Posted June 8, 2010 Share Posted June 8, 2010 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, "|||")); Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069512 Share on other sites More sharing options...
ignace Posted June 8, 2010 Share Posted June 8, 2010 Why wouldn't wordwrap work? $strs = explode("\n", wordwrap($str, 25, "\n")) Never underestimate the power of PlaystationHP Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069519 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069572 Share on other sites More sharing options...
dabaR Posted June 8, 2010 Share Posted June 8, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069574 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 Aaaahhh. Got it. Then it doesn't matter what kind of bad data has been entered, correct? Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069585 Share on other sites More sharing options...
dabaR Posted June 8, 2010 Share Posted June 8, 2010 Doesn't matter in terms of splitting it into two parts....if you need to scrub out characters, then you might need str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069592 Share on other sites More sharing options...
hadoob024 Posted June 8, 2010 Author Share Posted June 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204190-how-to-split-a-string-cleanly-into-at-most-2-strings/#findComment-1069595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.