Search code examples
phpreplacespecial-characters

Removing apostrophe from string leaves 39


I'm trying to remove special characters from a string, preparing it for use in a URL. This is my code as of now:

public function __construct($string) {

    $remove = array("!", "'", '"', "(", ")", ";", "@", "&", "=", "$", ",", "/", "?", "%", "#", "[", "]");

    var_dump($string);
    $this->string = $string;
    $this->string = str_replace($remove, "", $this->string);
    //$this->string = preg_replace("/[\s_\-:+]+/", "-", strtolower($this->string));
    var_dump($this->string);

}

And this is what it outputs:

string(16) "Today's Quiz" string(13) "Today39s Quiz"

That makes no sense... At no point do I convert special characters or anything, so where does the 39 come from? I can't track it down.


Solution

  • As shown by the length of the string, your input is actually:

    Today's Quiz
    1234567890123456 -> 16 characters
    

    Since you're trimming out & # and ; characters, the result is the remaining 39.