I think it is better to use multiline
flag with ^
:
$questions = preg_split('/^ *[0-9]+\. +/m', $_POST[test]);
Question
I am working on some code to break down the full text of a test that will copied and pasted with the following format:
1. This is question number one.
A. Answer 1
B. Answer 2
C. Answer 3
D. Answer 4
2. This is question number two.
3. This is another question, number three.
45. Ken has uses his money, $353. How much does he have after spending $214.
I am using the following preg_split:
$questions = preg_split("/[0-9]+\./", $_POST[test]);
My problem has come in with questions like #45 where there are numbers in the question itself and they are followed by a period.
I just want to match the numbers 1-100 followed by a period. Eg.
1. 2. 3. 4. 5. etc
Solution
I think it is better to use multiline
flag with ^
:
$questions = preg_split('/^ *[0-9]+\. +/m', $_POST[test]);
OTHER TIPS
A number between 1 and 100, followed by a period, can be matched by
/\b(?:100|[1-9][0-9]?)\./
but if the actual rule is to match a number at the start of a line, use
/^\d+\./m
You can use preg_match_all()
instead:
preg_match_all('~(?:^|\R)[0-9]+\. \K.+~', $_POST['test'], $matches);
$questions = $matches[0];
Use ^ to specify that it's the beginning of the line, using the g
and m
modifiers to specify global and multiline:
/^[0-9]+\.\s/m