PHP Use of regular expression ereg_replace smartly

I was working on the application and came accross a situation where I need to replace all character except the numeric ones. My text pattern was something similar to varchar(20). I initial thought to use substr, later moved to explode using “(” token. I suddenly strike is ereg_replace can help me to do this. After writing one line of code I am happy it solve the purpose.

echo ereg_replace(“[a-zA-Z()]”,””,”varchar(20)”);

[a-zA-Z()] searches for all the occurences in the string and replace them with supplied character.

I very hardly uses ereg_replace but from now onwards I will encourage myself and others to use ereg_replace.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: