Regular expression problem (for preg_split)

The person who created this database decided to create columns with several values ​​for “subjects” with each value written as an ordered list, that is, “1. [subject] 2. [other question] 3. [third question]”, etc. d. I want to create an array of each item, so I need to break these values ​​down into separate items.

$subjects = preg_split("[0-9]+\.\s", $subject);

When I run this, I get a Warning: preg_split () [function.preg-split]: Unknown modifier "+".

What am I doing wrong?

+5
source share
4 answers

You forgot the delimiters:

$subjects = preg_split("/[0-9]+\.\s/", $subject);

Also, pat this guy. Hard

+10
source

, php , [ ].

, ,

$subjects = preg_split("~[0-9]+\.s~", $subject);

+1

PHP PCREs . /, :

preg_split('/[0-9]+\.\s/', $subject);
//          ^          ^

, PHP [] .

:

Array
(
    [0] => 
    [1] => [subject] 
    [2] => [another subject] 
    [3] => [a third subject]
)

(unset($subjects[0])).


preg_match_all :

$str = "1. [subject] 2. [another subject] 3. [a third subject]";

preg_match_all('/\[([^\]]+)\]/', $str, $matches); 

$subjects = $matches[1];
// or $subject = $matches[0]; if you want to include the brackets.

$matches

Array
(
    [0] => Array
        (
            [0] => [subject]
            [1] => [another subject]
            [2] => [a third subject]
        )

    [1] => Array
        (
            [0] => subject
            [1] => another subject
            [2] => a third subject
        )

)
+1

, :

/[0-9]+\.\s/
0

All Articles