954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

split string into array with 2 delimiters

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

Thyvo
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Use preg_split:

$arr = preg_split('\[!(FIRST|LAST|OPTIONAL)!\]', $text);
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

Use preg_split:

$arr = preg_split('\[!(FIRST|LAST|OPTIONAL)!\]', $text);


I never worked with preg_split before, I dont know how it works so I did not think about that, unfortinately I got this error when trying it out:

Warning: preg_split() [function.preg-split]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\test.php on line 18

Thyvo
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Oops, forgot the delimiters.

$arr = preg_split('/\[!(FIRST|LAST|OPTIONAL)!\]/', $text);
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

Oops, forgot the delimiters.

$arr = preg_split('/\[!(FIRST|LAST|OPTIONAL)!\]/', $text);


Thanks :D

Thyvo
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: