0

I'm trying to use this line of code to accept URL variables with spaces replaced by '%20' but it also has to go through a regex

$type=urldecode(ereg_replace("[^a-zA-Z0-9%]+", "", @$_REQUEST['type']));

The result is just the %20 removed instead of replacing with a space e.g JohnDoe not John Doe, from John%20Doe

3
  • Your could would replace everything matching by your regex with nothing (""). Commented Apr 5, 2012 at 9:53
  • 1
    The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results. Commented Apr 5, 2012 at 9:54
  • The answer is $type=ereg_replace("[^a-zA-Z0-9[:space:][:blank:]]+", "", @$_REQUEST['type']); As mentioned it has to go through the regex for other reasons, $_REQUEST is decoded by default Commented Apr 5, 2012 at 10:30

3 Answers 3

1

You have the order wrong and note that ereg_replace is depreciated .. preg_replace is a better option

Try

 $type  = ereg_replace("[^a-zA-Z0-9%]+", "", urldecode(@$_REQUEST['type']));

Better Approch

$type = @$_REQUEST['type'] ; 
$type = urldecode($type);
$type = str_replace(" ","",$type) ;

Thanks

:)

Sign up to request clarification or add additional context in comments.

Comments

0

If you are just trying to replace space with %20 then use:

str_replace(" ", "%20", $_REQUEST['type']);

no need to use regex here.

1 Comment

Wrong parameter order. str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
0

$type = str_replace("%20", "", @$_REQUEST["type"]); should work and would replace all %20 by "".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.