nestiq Posted June 6, 2011 Share Posted June 6, 2011 Hello, I looking for a piece of code to chop a string into a array with 2 or more delimiters example: this is the text and I want to chop [!FIRST!] here and a bit later [!LAST!] here and if its posible (its not required) [!OPTIONAL!] chop it here and store this in a array like array( [0] => "this is the text and I want to chop " [1] => "[!FIRST!]" [2] => " here and a bit later " [3] => "[!LAST!]" [4] => " here and if its posible (its not required) " [5] => "[!OPTIONAL!]" [6] => " chop it here" ) If someone can help me with it please grtz Nestiq Quote Link to comment https://forums.phpfreaks.com/topic/238555-split-string-into-array-with-2-delimiters/ Share on other sites More sharing options...
fugix Posted June 6, 2011 Share Posted June 6, 2011 this is assuming that your string is separated by spaces. function addChars($str) { $ex = explode(" ", $str); foreach($ex as $key=>$values) { $values = "[!" . $values . "!]"; } } this will however add those two chars to all of your array values..since im not 100% sure what exactly it is that you are asking... Quote Link to comment https://forums.phpfreaks.com/topic/238555-split-string-into-array-with-2-delimiters/#findComment-1225893 Share on other sites More sharing options...
nestiq Posted June 6, 2011 Author Share Posted June 6, 2011 this will however add those two chars to all of your array values..since im not 100% sure what exactly it is that you are asking... I'm trying to find a way to isolate a part of the string in this case its tis part is seperated in the string by [!FIRST!] and [!OPTIONAL!] the part I wish to use is " here and a bit later " the other parts of the string are useless so I'm gona delete that afterwards Quote Link to comment https://forums.phpfreaks.com/topic/238555-split-string-into-array-with-2-delimiters/#findComment-1225905 Share on other sites More sharing options...
nestiq Posted June 6, 2011 Author Share Posted June 6, 2011 nevermind, I got it solution: $arr = preg_split('/\[!(FIRST|LAST|OPTIONAL)!\]/', $text); btw for some reason, I cant find the edit button in the post above :/ Quote Link to comment https://forums.phpfreaks.com/topic/238555-split-string-into-array-with-2-delimiters/#findComment-1225913 Share on other sites More sharing options...
fugix Posted June 6, 2011 Share Posted June 6, 2011 nevermind, I got it solution: $arr = preg_split('/\[!(FIRST|LAST|OPTIONAL)!\]/', $text); btw for some reason, I cant find the edit button in the post above :/ glad you figured it out...i was next going to suggest you use a preg function to isolate your values...and yes the edit button only allows you to edit a post for around 10 minutes i believe. Edit: please mark this thread as solved on the lower left of the thread Quote Link to comment https://forums.phpfreaks.com/topic/238555-split-string-into-array-with-2-delimiters/#findComment-1225927 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.