regex - PHP Regexp strtolower function that maintains case of text within quotes -


i search method lower string not change case of text within quotes.

the string :

select * utilisateurapplicatif idutilisateurapplicatif <> "-1" , identification = "toto" , motdepasse = "tototutu" , actif = 1 

the result why want :

select * utilisateurapplicatif idutilisateurapplicatif <> "-1" , identification = "toto" , motdepasse = "tototutu" , actif = 1 

you can using preg_replace_callback allows apply function on match result:

$subject = <<<'lod' select * utilisateurapplicatif idutilisateurapplicatif <> "-1" , identification = "toto" , motdepasse = "toto\"tutu" , actif = 1 lod;  $pattern = <<<'lod' ~ (?(define)      (?<dquotedcontent>         (?> [^"\\]++ | (?:\\{2})++ | \\. )*     ) ) " \g<dquotedcontent> " \k | [a-z]++ ~x lod;  $result = preg_replace_callback($pattern,     function ($match) { return strtolower($match[0]); },     $subject); print_r($result); 

pattern explanation:

the idea of pattern match quoted parts before , remove them match result not apply strtolower.

first define subpattern (dquotedcontent) possible content between double quotes, ie:

  • all characters not double quote or backslash [^"\\]
  • all number of backslashes (?:\\{2})++ (which can't escape anything)
  • escaped characters (an escaped double quote can't close quoted string)

the main part of pattern easy write:

" \g<dquotedcontent> "      # quoted part \k                          # reset have been matched before |                           # or [a-z]++                     # uppercase letters 

note \k useful since remove quoted part match. callback function don't have know have been matched apply strtolower.

notice: have written pattern using nowdoc syntax, define section, named subpattern, , comment mode (~x) more readability, can use instead same pattern in more compact version:

$pattern = '~"(?>[^"\\\]++|(?:\\\{2})++|\\\.)*"\k|[a-z]++~'; 

unlike nowdoc syntax, backslash must escaped twice.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -