user3770579
user3770579

Reputation: 23

PHP Remove spaces and %20 within single function

I wish to remove white space from a string. The string would have ben urlencoded() prior, so I also wish to remove %20 too. I can do this using two separate functions, but how do i do this with one function?

$string = str_replace("%20","",$string);
$string = str_replace(" ","",$string);

Upvotes: 0

Views: 576

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89574

Don't use a regex for that but strtr:

$result = strtr($str, array('%20'=>'', ' '=>''));

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174766

You could use preg_replace function.

preg_replace('~%20| ~', "", $string)

Upvotes: 1

Related Questions