Unstoppable Domains โ€” Get your daily AI drops report

Seeking php function to strip invalid characters from string

NamecheapNamecheap
Watch

RJ

Domain BuyerTop Member
Impact
3,210
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
•••
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
•••
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
•••
no probs -RJ- glad to be of assistance.
 
0
•••
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
•••
are the variables right? You had $string in your first post, but now it is $domain_name - is this right?
 
0
•••
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
•••
sorry can't help you then. I just use cleanstring() :lol:
 
0
•••
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
•••
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
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
CatchedCatched
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back