Forum Moderators: coopster

Message Too Old, No Replies

Removing Multiple Characters

         

Seraph

3:48 am on Dec 19, 2006 (gmt 0)

10+ Year Member



Hi all, I'm still sort of new to the whole PHP coding and I was wondering if there was a way to filter multiple characters in a string

For Example if someone entered OMG!!!!!, it would filter it o become OMG!

Any help would be appreciated, thanks :)

eelixduppy

4:14 am on Dec 19, 2006 (gmt 0)



What do you mean by character? Only non-alphanumeric characters?

Here's something to get you started that I wrote up real quick:


function Test_Function($match)
{
return $match[1];
}
$string = "OMG! there''''s an eeerooor in this code but maybe it will guide you?";
$pattern = "/([^\w\s])+/";
echo [url=http://us2.php.net/manual/en/function.preg-replace-callback.php]preg_replace_callback[/url]($pattern,"Test_Function",$string);

You may already see the problem; if there are two non-alphanumeric characters next to each other, the latter will be erased. Mess around with it to see what you can get.

...and welcome to webmasterworld! :)

mcibor

12:18 pm on Dec 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's hard to accomplish.

To remove multiple! you can use

$text = preg_replace( "/\!{1,}/", "!", $text );

To remove all doubles (and only doubles) I don't know how to perform that.

Michal

coopster

10:24 pm on Dec 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Related threads:
Check text string for duplicates [webmasterworld.com]
Checking string for consecutive alpha numerics [webmasterworld.com]

Seraph

10:32 pm on Dec 19, 2006 (gmt 0)

10+ Year Member



Thanks for your help :)