Search code examples
phpstringurlencode

How to URL-encode a string


$flr = preg_replace("/\\\'/","%27",$flr);

If URL has this symbol: ' it got replaced by %27 and the URL should become http://localhost/%27, but this doesn't work.

For example:

$flr = preg_replace("/\\\"/","%22",$flr);

URL replacement works and I get http://localhost/%22

But then why doesn't my first example work?

To test, I'm using:

function isValidFLR(&$flr) {
    $flr = preg_replace("/\\\'/","%27",$flr);
    $flr = preg_replace("/\\\"/","%22",$flr);
    echo $flr;
    die();
}

Solution

  • $flr = preg_replace("/'/","%27",$flr);
    

    will work.

    But, as others have said, one of the PHP functions which does this could be a better solution.