etrader Posted January 21, 2011 Share Posted January 21, 2011 I have a $string with this structure "word-word2 something-SECONDPART". I want to divide it to $string1 and $string2 by separating with the last "-". The point is that "-" exists in the first part, and I just want to make the second string after the last "-" Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/ Share on other sites More sharing options...
JakeTheSnake3.0 Posted January 21, 2011 Share Posted January 21, 2011 $tmp = explode('-', $string); $string1 = $tmp[0] . '-' . $tmp[1]; $string2 = $tmp[2]; unset($tmp); Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163180 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 Is it possible for there to be a total number of hyphens in the string other than 2? $string = "word- word2- - - - - - - something -SECONDPART"; if( strpos($string, '-') !== FALSE ) { //$string = "wordword2 something SECONDPART"; $substr1 = substr( $string, 0, (strrpos($string, '-') ) ); $substr2 = substr( $string, (strrpos($string, '-') + 1) ); } else { $substr1 = $string; $substr2 = ''; } echo "<br>First part: $substr1<br>Second part : $substr2"; Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163201 Share on other sites More sharing options...
etrader Posted January 21, 2011 Author Share Posted January 21, 2011 $tmp = explode('-', $string); $string1 = $tmp[0] . '-' . $tmp[1]; $string2 = $tmp[2]; unset($tmp); It works well; but a question out of curiosity. What is the role of unset? Is it possible for there to be a total number of hyphens in the string other than 2? Yes, we deal with the last "-"; it may happen several times. Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163208 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 Wait a minute, do you only need the part after the last hyphen, or do you need both parts? Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163211 Share on other sites More sharing options...
etrader Posted January 21, 2011 Author Share Posted January 21, 2011 Actually, I need to split the string to two new ones, as I can handle both of them (Yes, I need both of them). The solution provided by JakeTheSnake3.0 was very simple and thus attracted my attention; however, it only works for the first part (returning the string before the last "-"). Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163213 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 The code I posted above works, but I actually forgot all about array_pop. This code will work just as well. $string = "This is- the str - ing for- test-ing and -doing stuff -SECONDPART"; // Test string $arr = explode('-', $string); $suffix = array_pop($arr); $prefix = implode('-', $arr); echo "<br>Prefix: $prefix<br>Suffix: $suffix"; // Everything b4 last hyphen is in $prefix, everything after in $suffix. Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163216 Share on other sites More sharing options...
etrader Posted January 21, 2011 Author Share Posted January 21, 2011 Thanks Pikachu2000! This is exactly what I meant: simple and effective Thanks a million! Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163220 Share on other sites More sharing options...
mikosiko Posted January 21, 2011 Share Posted January 21, 2011 Maybe? $string = "This is- the str - ing for- test-ing and -doing stuff -SECONDPART"; // Test string echo "<br />String 1 :" . substr($string,0,strrpos($string, '-')); echo "<br />String 2 :" . substr($string,strrpos($string, '-')+1); scratch this... I didn't see Pika reply #2 Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163253 Share on other sites More sharing options...
etrader Posted January 21, 2011 Author Share Posted January 21, 2011 WOW! very simply and handy Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/225225-parse-an-string-by-a-character/#findComment-1163303 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.