preg_match 在 PHP 中用于“&$”

preg_match in PHP for "&$"

我正在尝试为包含特殊字符的字符串实现 preg_match:“&$”。

我使用的代码:

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

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

我已经试过了preg_match("/&\$/",$lines,$matches)

结果我一无所获。 (我正在使用的与“$path”相关的文件包含 2 行包含“&$”)。

请帮忙!!

您在双引号模式中使用了美元表达式。

以下对我有用:

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

检查这个关于单引号和双引号之间区别的现有问题: What is the difference between single-quoted and double-quoted strings in PHP?