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?
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.
$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.
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.
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.
You forgot the delimiters:
$subjects = preg_split("/[0-9]+\.\s/", $subject);
Also, slap that guy. Hard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With