Welcome to WebmasterWorld Guest from 54.147.50.227

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Preg replace help?

     
5:59 am on Dec 14, 2010 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
posts:232
votes: 0


Hi,
i need help with my preg_replace, basically i am trying to replace everything except RFC 3986 valid chars


$User_URL=preg_replace('/[^A-Z, a-z, 0-9, -, ., _, ~, :, /, ?, #, [, ], @, !, $, &, ', (, ), *, +, ,, ;,=,%]/', '', $User_URL);



Thanks
8:37 am on Dec 14, 2010 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


You need to escape special characters which have meaning within regular expressions, also you are not actually building an array here, so no need to seperate each character with a comma. So I think this should cover it:

$User_URL=preg_replace('/[^A-Za-z0-9\-\._\~\:\/\?#\[\]@\!\$&\'\(\)\*\+,;\=%]/', '', $User_URL);

Though I would have to ask, won't the replacement butcher the url? Would you not be able to just use rawurlencode() [php.net] or urlencode() [php.net] instead?