Seeking php function to strip invalid characters from string

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
Watch

RJ

Domain BuyerTop Member
Impact
3,227
I'm looking for the most efficient function that I can give a string and have it return the same string without the invalid characters. Its for a domain related application so only alphanumeric and hyphens are valid.

Example:
$string = cleanstring("test_ing"); # returns "testing"
$string = cleanstring("t@e#s%t i*n^g"); # returns "testing"
$string = cleanstring("test-ing"); # returns "test-ing"

Suggestions?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
wouldnt you just be as well to do :-

$domain_name = preg_replace('/[^a-zA-Z0-9/-]/','',$domain_name);
 
0
Close
filth said:
wouldnt you just be as well to do :-

$domain_name = preg_replace('/[^a-zA-Z0-9/-]/','',$domain_name);

That's what I needed. Thanks Peter!
 
0
Close
no probs -RJ- glad to be of assistance.
 
0
Close
I'm getting this error

Warning: Unknown modifier '-' in /script.php on line 85

Using this line
$domain_name = preg_replace('/[^a-zA-Z0-9/-]/','',$domain_name);
 
0
Close
are the variables right? You had $string in your first post, but now it is $domain_name - is this right?
 
0
Close
Variable name shouldn't matter, but my actual code is

$sld = preg_replace('/[^a-zA-Z0-9/-]/','',$sld);

I think I just need a refresher on regular expressions.
 
0
Close
sorry can't help you then. I just use cleanstring() :lol:
 
0
Close
It seems that if you remove the / before the -, the error goes away, but I dont know if thats still what you want. I never really got the hang of reg exps so sorry if that wasnt what you were looking for
 
0
Close
yes my bad I escaped the - when it wasnt needed. RJ you can just delete the / before the - and it will work

Poordoggie cleanstring() is not an in built php function but I presume you mean

PHP:
function cleanString($wild) {
   return ereg_replace("[^[:alnum:]+]","",$wild);
}

which is in a note by one of the users at php.net
 
Last edited:
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Spatial Sidebar: More consistent display
We fixed some bugs in the Spatial Sidebar. It now displays more consistently across browsers and should perform better. If you use it...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back