0

I'm trying to encode a feed's URLs, which unfortunately contain Unicode characters. My feed reader doesn't support this.

My knowledge in php is limited and my basic approach failed when it encountered a French character ç. Clearly listing each character is slow and too much work:

    public function encodeUmlauts($req_url) {
        $req_url = str_replace("ö", urlencode("ö"), $req_url);
        $req_url = str_replace("ü", urlencode("ü"), $req_url);
        $req_url = str_replace("ä", urlencode("ä"), $req_url);
        $req_url = str_replace("Ö", urlencode("Ö"), $req_url);
        $req_url = str_replace("Ü", urlencode("Ü"), $req_url);
        $req_url = str_replace("Ä", urlencode("Ä"), $req_url);
        $req_url = str_replace("ß", urlencode("ß"), $req_url);

        return $req_url;
    }

I assume something can be done with urlencode and/or parse_url, but I don't see an obvious solution.

What I'm looking for is similar to this stackoverflow question, but for PHP.

1
  • print urlencode("garçon"); will output gar%C3%A7on Commented Dec 7, 2019 at 11:26

1 Answer 1

1

This will urlencode all non-ascii characters in the URL string:

$url = preg_replace_callback('/[^\x20-\x7f]/', function($match) {
    return urlencode($match[0]);
}, $url);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.