Re: preg_match() is returning inaccurate result

Jigar Dhulla <[email protected]>
Newsgroups gmane.comp.php.general
Message-ID <CAO=0E+x7b-r1HC5jqdhutTCHAJOnBPWah+TOgJt2cZDkLnfWfg@mail.gmail.com>
On Tue, Apr 23, 2024 at 8:02 AM <[email protected]> wrote:

> PHP8.3.4
>
> Can someone help? preg_match() is acting weird about \$ and \^.
>
> var_dump(preg_match("/\$third-party$/",'xyz$third-party')?true:false);//
> returns FALSE, really expecting TRUE here!
> var_dump(preg_match("/$third-party$/",'xyz$third-party')?true:false);//
> returns TRUE???
>
> ^ and $ must escape if it is not in beginning/ending position.
> Isn't this correct?
>
> At least Javascript is answering correctly.
>
> /\$third-party$/.test('xyz$third-party'); // TRUE
> /$third-party$/.test('xyz$third-party'); // FALSE
>
Hi,

double quotes ("") around a string are evaluating `$third` as a variable in
the first argument.

Use single quotes ('') instead.
var_dump(preg_match('/\$third-party$/','xyz$third-party')?true:false);
var_dump(preg_match('/$third-party$/','xyz$third-party')?true:false);

Playground: https://onlinephp.io/c/52068
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.