Jens Törnell
Jens Törnell

Reputation: 24798

Find content between both single and double quotes with PHP

My text "can contain" both single 'and double"' quotes. The quotes "can also be 'nested" as you can see.

Expected results

(array with 3 items)

can contain
and double"
can also be 'nested

How far I've come

I'm not a regex expert, far from it. I still managed to get text between double quotes like I can "grab this" text.

preg_match_all("~\"(.*?)\"~", $text, $between);
print_r($between);

Valid / invalid

Additional notes

My guesses

My guess is that each character needs to be looped and checked. If it starts with a ", it needs to step the characters to the next " in order to wrap it in. Then I guess it's need to be reset from that position to see what the next type of quote is and to it again until the string has ended.

Answers on Stackoverflow that does not work

This answer does not work for my problem: regex match text in either single or double quote

A proof can be seen here: https://regex101.com/r/OVdomu/65/

Upvotes: 4

Views: 641

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627545

You may use

if (preg_match_all('~(?|"([^"]*)"|\'([^\']*)\')~', $txt, $matches)) { 
    print_r($matches[1]);
}

See the regex demo and the PHP demo.

A variation that supports escaped quotes, too:

'~(?|"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')~s'

See this regex demo.

The (?|"([^"]*)"|\'([^\']*)\') is a branch reset group matching either ", then any 0+ chars other than " and then a " or a ', then any 0+ chars other than ' and then ', while capturing into Group 1 all the contents between the matching quotation marks.

Upvotes: 1

Related Questions