Re: ereg_replace help wanted

From: Date: Wed, 29 Sep 2004 14:33:34 +0000
Subject: Re: ereg_replace help wanted
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-24664@lists.php.net to get a copy of this message
On 29 Sep 2004 George Pitcher wrote: > The problem is that I think that some of my users are pasting the range in, > rather than re-typing (I don't really want to stop them from pasting) and I > am sure that sometimes the '-' is coming over as something other than a > conventional hyphen. > > Can anyone suggest a filter that will handle my hyphens correctly, > regardless of what type of hyphen the user is entering? You probably don't need ereg_replace (though if I did, I'd actually use preg_replace), you can probably do it more simply with str_replace. But first you have to know what characters you're actually getting. Say they are '*', '_', and '~' (just an example), then you can do it easily with: $newstring = str_replace(array('*', '_', '~'), array('-', '-', '-'), $string); Or with: $newstring = preg_replace('/[*_~]/', '-', $string); Another approach would be to replace all non-alphanumeric characters (other than commas) with a '-', then you are treating all punctuation characters as dashes: $newstring = preg_replace('/[^a-zA-Z0-9,]/', '-', $string); But that might be overkill ... -- Tom

« previous php.windows (#24664) next »