0

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

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Thyvo
0

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

1

Oops, forgot the delimiters.

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

Oops, forgot the delimiters.

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

Thanks :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.