# simple question : regular expressions

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
i have a string that looks something like this:
-1--3--8--22--550--1028-

how to make it an array like this:
0 => 1,
1 => 3,
2 => 8,
3 => 22,
4 => 550,
5 => 1028

also, i would like to know some good reg exp tutorials, books or whatever that start from basics and that can teach me reg exp

Thank you

## Re: simple question : regular expressions

<?php

\$string = '-1--3--8--22--550--1028-';
\$array = explode( '--', trim( \$string, '-' ) );
var_dump(\$array);

?>

Will do the trick.

info.

## Re: simple question : regular expressions

amygdala wrote:

Just found out...

preg_split("%-%",\$string_to_search,-1,PREG_SPLIT_NO_EMPTY);

## Re: simple question : regular expressions

friglob wrote:

preg_split("/-+/", \$string_to_search)  is a somewhat more orthodox
solution.

## Re: simple question : regular expressions

\$string = "-1--3--8--22--550--1028-";

if(ereg
("-([0-9])--([0-9])--([0-9])--([0-9])--([0-9])--([0-9])-",
\$string, \$regs)){
\$array_set = array_splice(\$regs,1);
print_r(\$regs);
print_r(\$array_set); // <- This is the output requested.
}

Chung Leong wrote:

## Re: simple question : regular expressions

Don't toppost

wegenern@gmail.com wrote:

("-([0-9])--([0-9])--([0-9])--([0-9])--([0-9])--([0-9])-",

But a lot more undynamic & slower, I'd suggest using Chung's solution. It's
the fastest and the easiest altered.

Grtz,
--
Rik Wasmus