dennismonsewicz Posted March 4, 2010 Share Posted March 4, 2010 I have a string I want to break apart at a certain limit and then set the two parts into different variables... I know I can use the explode functionality but am confused as to how to break the string apart... any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/ Share on other sites More sharing options...
Dennis1986 Posted March 4, 2010 Share Posted March 4, 2010 Use this function if you want to specify the length of the split: http://php.net/manual/en/function.substr.php echo substr('abcdef', 1); // bcdef echo substr('abcdef', 1, 3); // bcd echo substr('abcdef', 0, 4); // abcd echo substr('abcdef', 0, ; // abcdef echo substr('abcdef', -1, 1); // f Or use explode as you meantioned if you use a specific delimiter: http://php.net/manual/en/function.explode.php $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021676 Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 Use substr() $limit = 10; $firstpart = substr($string, 0, $limit); //The first 10 characters $secondpart = substr($string, $limit); //The remaining characters Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021677 Share on other sites More sharing options...
dennismonsewicz Posted March 4, 2010 Author Share Posted March 4, 2010 Use substr() $limit = 10; $firstpart = substr($string, 0, $limit); //The first 10 characters $secondpart = substr($string, $limit); //The remaining characters THANKS!!!! This works like a charm! Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021684 Share on other sites More sharing options...
dennismonsewicz Posted March 4, 2010 Author Share Posted March 4, 2010 Ok so I got it working based on the latest solution... Basically I have a writable PDF that I am creating based on user generated information from the web. The PDF has a two part question and I am taking the user input from a textarea that I am then breaking apart and placing on the PDF in two separate parts. But my dilemma is what if the last part of the first section contains a word that breaks in half (IE: description, between, because, etc.) and then the word continues on the second line. I am wondering if I could split the last word on the first line and place a dash in the word to let others reading it know that the word continues on the second line... Make sense? Sorry if that is confusing... its hard to place what I need help on to words lol Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021695 Share on other sites More sharing options...
Dennis1986 Posted March 4, 2010 Share Posted March 4, 2010 Plenty of examples for that in my first link http://www.php.net/manual/en/function.substr.php#93963 http://www.php.net/manual/en/function.substr.php#83585 http://www.php.net/manual/en/function.substr.php#80247 And more... Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021700 Share on other sites More sharing options...
dennismonsewicz Posted March 4, 2010 Author Share Posted March 4, 2010 Plenty of examples for that in my first link http://www.php.net/manual/en/function.substr.php#93963 http://www.php.net/manual/en/function.substr.php#83585 http://www.php.net/manual/en/function.substr.php#80247 And more... Awesome! Thanks! I will take a look at these examples! Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021709 Share on other sites More sharing options...
Psycho Posted March 5, 2010 Share Posted March 5, 2010 Plenty of examples for that in my first link http://www.php.net/manual/en/function.substr.php#93963 http://www.php.net/manual/en/function.substr.php#83585 http://www.php.net/manual/en/function.substr.php#80247 And more... I don't think those have anything to do with what the OP is asking for now. I believe the OP is now asking how to logically break a word at a syllable, i.e. hyphenate it. Example (lines 1 & 3): Toyota has for years blocked access to data stored in de- vices similar to airline "black boxes" that could explain crashes blamed on sudden unintended acceleration, accord- ing to an Associated Press review of lawsuits nationwide and interviews with auto crash experts. @OP: If you are creating the PDF file via the PHP PostScript functions there are a couple that may help: ps_show_boxed() and/or ps_hyphenate(). however, if you are creating the PDF through other means, e.g. writing raw postscript and distilling this may be more difficult as you will not "know" ahead of time where the word will need to break. @Dennis1986, I like how you updated your original post to include information about using substr() after I posted a solution using it. Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1021726 Share on other sites More sharing options...
Dennis1986 Posted March 5, 2010 Share Posted March 5, 2010 @Dennis1986, I like how you updated your original post to include information about using substr() after I posted a solution using it. What are you talking about? I modified my post because the editor added in some bbcode that shouldn't be there. Quote Link to comment https://forums.phpfreaks.com/topic/194183-breaking-string-apart-question/#findComment-1022111 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.