Skip to content
Advertisement

unable to remove special char % from url

I am trying to remove special character % from url.
I tried lot of solution like string_replace and etc. but not success.

I do not know what is wrong with my code:

<?php
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    if (strpos($url,'pages') !== false) {
        $url = trim($url, '%');
    }
?>

Can anybody help?

Advertisement

Answer

trim() – Strip whitespace (or other characters) from the beginning and end of a string

This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:

  • ” ” (ASCII 32 (0x20)), an ordinary space.
  • “t” (ASCII 9 (0x09)), a tab.
  • “n” (ASCII 10 (0x0A)), a new line (line feed).
  • “r” (ASCII 13 (0x0D)), a carriage return.
  • “” (ASCII 0 (0x00)), the NUL-byte.
  • “x0B” (ASCII 11 (0x0B)), a vertical tab.

FOR URL

You can use urldecode to convert the URL string.

echo urldecode($your_url);

OR Use this function for Cleaning your URL :

function PerfectURL($your_url) {
  $perfect_url = strtolower(preg_replace( array('/[^a-z0-9- ]/i', '/[ -]+/'), array('', '-'), $your_url));
            return $perfect_url;
     }

OR Replace Only % by ''

str_replace("%", "", $your_url);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement