在php中编码包含连字符的网址

I have the following URL: http://test.com/client/!MG2s-/

I want to be able to grab the slug and use it further down in my code

With the following code:

$page_slug=$post->post_name; echo $page_slug;

the output is !mg2s

Even if the url is http://test.com/client/%21MG2s%2D/ the result is still the same. The slug is generated from a third party app, so the hyphen is required. I understand that dashes are used to sanitize titles, but I need to find a way to display the hyphen.

I have also tried with no success:

$page_slug= urlencode($post->post_name); $str = str_replace('-', '%2D', $page_slug); echo $str;

Is there any way that the hyphen can be displayed?

FURTHER NOTE: This is only happening to slugs ending with hyphens. Slugs with hyphens in between have no issues displaying the hyphens.

Found the solution here

$url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path=parse_url( $url, PHP_URL_PATH );
$page_slug=pathinfo( $url_path, PATHINFO_BASENAME );

instead of using

$page_slug=$post->post_name;