Forum Moderators: coopster

Message Too Old, No Replies

How to strip spaces from string using php - is ereg_replace it?

Strip string spaces php ereg_replace

         

philzac

8:48 am on Aug 10, 2004 (gmt 0)



I want to strip out spaces from php form data - a supplied sample could be "33 44 55 66" when I want "33445566" - before saving to the mysql database.

Can anyone please help me with ereg_replace coding, or other php code, to achieve this. I just cannot make sense of the vast amount of material around.

The variable containing the data to be stripped of all spaces is $buscode.

Thanks!

TheBlueEyz

11:30 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Don't ever use ereg functions if you can keep from it - they incurr alot of overhead that you don't want to have unless it's necessary.

In most cases, anywhere you need to use ereg functions, there is probably a preg_match alternative you can use. The preg_match functions are also faster.

In THIS case however - you don't need either.

How about this:

$newvar = str_replace(' ', '', $buscode);

Very simple.

Ok ok ok, here's how you do it with ereg and preg:

ereg_replace(' ', '', $buscode);

preg_replace('/ /', '', $buscode);

preg is a "perl compatible regular expression".

jatar_k

4:01 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



and Welcome to WebmasterWorld philzac