最后匹配$

I want to match a $ only at the end.

Why does it does not work:

<?php

$reg = '{$$}';
$str= 'helloc$a';
print preg_match($reg,$str);

It prints 1 -- matched. But I want it to match for example inputs like abc$ or zzz$ only.

$ is a meta-character in regular expressions and has a special meaning — it asserts the position at the end of a line. When you want to match a literal $, you'll need to escape it, i.e. use \$ instead of $:

$reg = '{\$$}';

As Casmir notes in the comments section below the answer, this pattern will also match when the last $ is immediately followed by a newline. To prevent this, you can use the following pattern instead:

$reg = '{\$$}D';

With the D modifier set, a dollar metacharacter in the pattern matches only at the end of the given string. If this modifier is not set, $ also matches immediately before the final character if it is a newline character.

$ is a special character in PHP. You should add a \ before it . Try this: $reg = '/\$$/';