php - Get html or text from inside quotes including escape quotes with RegEx -



php - Get html or text from inside quotes including escape quotes with RegEx -

what want attribute value simple text i'm parsing. want able contain html within quotes, that's got me stalling right now.

$line = 'attribute = "<p class=\"qwerty\">hello world</p>" attribute2 = "value2"'

i've gotten point (substring) i'm getting value

$line = '"<p class=\"qwerty\">hello world</p>" attribute2 = "value2"'

my current regex works if there no escaped quotes within text. however, when seek escape html quotes, doesn't work @ all. also, using .* going end of sec attribute. i'm trying obtain string above is

$result = '<p class=\"qwerty\">hello world</p>'

this how far i've gotten trial , error regex-ing.

$value_regex = "/^\"(.+?)\"/" if (preg_match($value_regex, $line, $matches)) $result = $matches[1];

thank much in advance!

you can utilize negative lookbehind avoid matching escaped quotes:

(?<!\\)"(.+?)(?<!\\)" regex demo

here (?<!\\) negative lookbehind avoid matching \".

however caution on using regex parse html, improve utilize dom that.

php code:

$value_regex = '~(?<!\\\\)"(.+?)(?<!\\\\)"~'; if (preg_match($value_regex, $line, $matches)) $result = $matches[1];

php regex parsing lexer

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -