简体   繁体   中英

preg_match in PHP for “&$”

I am trying to implement preg_match for string containing special characters : "&$".

The code that i am using :

$f = fopen($path,"r");

while($lines = fgets($f))
{
    if(preg_match("/\&\$/",trim($lines),$matches))
    {
        echo "Yes<br>";
    }
}

I have already tried preg_match("/&\\$/",$lines,$matches)

I am getting nothing as a result. (The file related to "$path" that i am using contains 2 lines containing "&$").

Plz help !!

You are using a dollar expression within the double-quoted pattern.

The following works for me:

if(preg_match('/&\$/',trim($lines),$matches))

Check this existing question on the difference between single and double quotes: What is the difference between single-quoted and double-quoted strings in PHP?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM