Regex to match double quoted strings without variables inside php tags -
basically need regex expression match double quoted strings inside php tags without variable inside.
here's have far:
"([^\$\n\r]*?)"(?![\w ]*')
and replace with:
'$1'
however, match things outside php tags well, e.g html attributes.
example case:
<a href="somelink" attribute="value">here's "dog's website"</a> <?php $somevar = "someval"; $somevar2 = "someval's got quote inside"; ?> <?php $somevar3 = "someval $var inside"; $somevar4 = "someval " . $var . 'with concatenated' . $variables . "inside"; $somevar5 = "this php tag doesn't close, it's end of file...";
it should match , replace places "
should replaced '
, means html attributes should ideally left alone.
example output after replace:
<a href="somelink" attribute="value">here's "dog's website"</a> <?php $somevar = 'someval'; $somevar2 = 'someval\'s got quote inside'; ?> <?php $somevar3 = "someval $var inside"; $somevar4 = 'someval ' . $var . 'with concatenated' . $variables . 'inside'; $somevar5 = 'this php tag doesn\'t close, it\'s end of file...';
it great able match inside script tags too...but might pushing 1 regex replace.
i need regex approach, not php approach. let's i'm using regex-replace in text editor or javascript clean php source code.
tl;dr
this complex complex done regex. not simple regex. might have better luck nested regex, need lex/parse find strings, , then operate on them regex.
explanation
you can probably manage this. can probably manage well, maybe perfectly. it's not going easy. it's going very difficult.
consider this:
welcome php file. we're not "in" yet. <?php /* ok. we're "in" php. */ echo "this \"stringa\""; $string = 'this \"stringb\"'; echo "$string"; echo "\$string"; echo "this still ?> php."; /* still ?> php. */ ?> we're <?="out"?> of php. <?php // here again, "in" php. echo <<<string how "you" want \""deal"\" string; string; echo <<<'string' apparently \\"nowdoc\\". i've never used it. string; echo "and \\" . "this? tricky '\"' catch?"; // etc...
forget matching variable names in double quoted strings. can match of string in example? looks nightmare me. so's syntax highlighting won't know it.
did consider variables may appear in heredoc strings well?
i don't want think regex check if:
- inside
<?php
or<?=
code - not in comment
- inside quoted quote
- what type of quoted quote?
- is quote of type?
- is preceded
\
(escaped)? - is
\
escaped?? - etc...
summary
you can write regex this. can manage backreferences , lots of time , care. it's going hard , going waste lot of time, , if ever need fix it, aren't going understand regex wrote.
see also
this answer. it's worth it.
Comments
Post a Comment