-4

Let's say I want to place the bold content into a variable called $question_text.

1} What color is the sky:
A) Answer A
B) Answer B
C) Answer C

Answer: A

How do I use preg_match to do that?

I tried:

if(preg_match("#}(.*)#", $question, $question_match))
{
    //Extract the question text
    $question_text = trim($question_match[1]);

But it only gives me the first line: **What color is the sky: **

What am I doing wrong?

stylusss
  • 79
  • 1
  • 9
  • what are you trying to return? –  May 22 '18 at 02:40
  • 1
    And what of this question? https://stackoverflow.com/questions/50458152/preg-match-with-regex – hungrykoala May 22 '18 at 02:43
  • I apologize, I though someone had deleted it, I could find it in my logs. – stylusss May 22 '18 at 02:45
  • I'm trying to return what's bolded: – stylusss May 22 '18 at 02:45
  • 1
    You are only matching lines that contain `}`, so of course the others don't match. – l'L'l May 22 '18 at 02:46
  • `preg_match` only gives 1 match. `preg_match_all` is for multiple. See https://regex101.com/r/zLYd6S/2/ vs https://regex101.com/r/zLYd6S/1/, assuming you correct the regex. Also you could just explode on new lines. You really aren't using regex power here. – user3783243 May 22 '18 at 02:46
  • Can you recommend an answer for me, as to how I'd store what's in bold in a variable? By the way, who keeps down-voting my posts?! – stylusss May 22 '18 at 02:48
  • 1
    You could use dot-all perhaps `#(?s)}(.*)#` which would put your question and answers in one array, although I'm not sure that's what you are looking for. – l'L'l May 22 '18 at 02:50
  • Where does `bold content` come from? How do you know where to end? We need more parameters to help. – user3783243 May 22 '18 at 02:51
  • Also, practice the regex here: https://regexr.com/ – hungrykoala May 22 '18 at 02:51
  • I'L'I is correct! Except, I want it not to include everything after and including **Answer:** – stylusss May 22 '18 at 02:54
  • `Answer:` is not in bold in your question. Please take time to format your question so it has all information. We are 11 comments in and finding new requirements. Maybe `\d}[\S\s]+?Answer: [A-Z]` I suspect there are more requirements though. – user3783243 May 22 '18 at 02:55
  • 2
    with every comment you add, its less clear what you actually want. –  May 22 '18 at 02:57
  • I want what's in **bold** to be placed in _$question_text_. Notice Answer: ... is not in bold. – stylusss May 22 '18 at 02:57
  • I want it **not** – stylusss May 22 '18 at 02:58

1 Answers1

-1
#(?s)}(.*)+?Answer: [A-Z]#

Worked for me!

stylusss
  • 79
  • 1
  • 9