Jump to content

Replacing multiple characters in a string with preg_replace.


paulferree

Recommended Posts

I'm trying to remove anything that isn't a number,letter, or underscore from a string.

 

I think I have my syntax wrong...this replacement code worked in ColdFusion, what is the correct way to write this in php?

 

Here is what I have (that isn't working):

 

preg_replace("[^0-9a-zA-Z_]", "", $the_variable);

 

Thanks!

Paul

Close

 

$this_var = preg_replace("/[^0-9a-zA-Z_]/", "", $the_variable);

 

Or easier

 

$this_var = preg_replace("/[^0-9a-z_]/i", "", $the_variable);

 

The 'i' switch forces a case-insensitive search.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.