使用PHP将%20转换为空格[复制]

This question already has an answer here:

I'm currently trying to pull the last words from the url after the slash and print them inside a H2 tag.

I'm using this line:

<h2>
    <?php 
        echo basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
    ?>
</h2>

Currently if after the slash there is multiple words I'm getting %20 in-between the words, how do I go about adapting my code to convert that into a space?

Any Advice or guidance is appreciated.

Cheers :)

</div>

You can use a Javascript function decodeURIComponent. The following

decodeURIComponent('foo%20bar')

produces "foo bar".

Or you can use a PHP approach by wrapping your code inside rawurldecode function.

Returns a string in which the sequences with percent (%) signs followed by two hex digits have been replaced with literal characters.

You can use urldecode(string) to decode a URL into their correct characters. It’s the opposite of urlencode(string).

NB Don’t use with $_GET, as that is already decoded and could cause issues.

See urldecode from the PHP manual for more.

Use urldecode(); in php

$a = basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$b = urldecode($a);
echo $b;

I think this should work.

echo preg_replace("/\s+/","",basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));