Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Regular Expression (for preg_split)

Tags:

regex

php

The person who designed this database decided to make a multi-value column for "subjects" with each value written like an ordered list, i.e. "1. [subject] 2. [another subject] 3. [a third subject]" etc. I want to make an array of every subject used, so I need to split these values into distinct subjects.

$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?

like image 352
tkm256 Avatar asked Mar 08 '11 19:03

tkm256


People also ask

How preg_ split() function works?

The preg_split() function is an inbuilt function in PHP which is used to convert the given string into an array. The function splits the string into smaller strings or sub-strings of length which is specified by the user. If the limit is specified then small string or sub-strings up to limit return through an array.

What does $1 do in regex?

$1 is the first group from your regular expression, $2 is the second. Groups are defined by brackets, so your first group ($1) is whatever is matched by (\d+). You'll need to do some reading up on regular expressions to understand what that matches.

What will the $' regular expression match?

By default, regular expressions will match any part of a string. It's often useful to anchor the regular expression so that it matches from the start or end of the string: ^ matches the start of string. $ matches the end of the string.

What does * in regular expression mean?

Example : The Regular expression . * will tell the computer that any character can be used any number of times. Optional character – ( ? ) This symbol tells the computer that the preceding character may. or may not be present in the string to be matched.


1 Answers

You forgot the delimiters:

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

Also, slap that guy. Hard.

like image 53
seriousdev Avatar answered Oct 20 '22 16:10

seriousdev