You are doing it wrong.
The original character '
is turned into %27
, and then the percent sign is turned into %25
. This second step must not happen.
If you want to encode everything else, and then also single quotes, first call encodeURI
, then replace.
That way, '
will return unaltered from escaping, and then be transformed into %27
, with no further changes.
PHP can then decode the string properly.