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
- Valid:
This is "A text"
(A text) - Valid:
This is 'A text'
(A text) - Valid:
This is "A 'text"
(A 'text) - Valid:
This is 'A "text'
(A "text) - Invalid:
This is "A text
(Uneven quotes 1) - Invalid:
This is 'A text
(Uneven quotes 1) - Invalid:
This is "A "text"
(Uneven quotes 3) - Invalid:
This is 'A 'text'
(Uneven quotes 3) - Invalid:
This "is ' A " text'
(Intersecting)
Additional notes
- If there is an error, like a non closed quote, it's fine if it breaks (
This "has "one wrong" quote
) - I would prefer a regex solution, but if there are better non regex solutions, it's fine.
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/