preg match - PCRE and PHP - Escaping meta-character -


i continue learn php , pcre. question today following:

<?php         $search = "money $ money $ money...";          /*i "invalid"*/         if(preg_match("&money \$&", $search)){               echo 'valid <br/>';         }else{               echo 'invalid <br/>';         }          /*i "valid"*/         if(preg_match('&money \$&', $search)){               echo 'valid <br/>';         }else{               echo 'invalid <br/>';         }     ?> 

i suppose when use double quotes instead of single ones php messing encoding of ascii character, not totally understand why.

can provide detailed answer?

single quote strings not processed (not extent of double quote strings, precise) , taken "as-is", when string specified in double quotes, more special characters available , variables parsed within it.

if dollar sign ($) encountered in double quoted string, parser greedily take many tokens possible form valid variable name. in double quote string in code, $ has special meanings , not represent character of $ (the character of \, has special meanings , used indicate start of escape sequences).

to specify literals of dollar sign , backslash in double quote strings, need use escape sequences of \$ , \\, respectively:

... // now, "valid" here if(preg_match("&money \\\$&", $search)) { ... 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -