php - Can't remove whitespaces in string -


my script reads txt file , writes values database table. there field price, , in txt file price 4 digit values have whitespace after first digit (2 500 ex.). put function (str_replace works , replaced ',' when use removing whitespace not it, , put preg_replace skips whitespace):

$model->price = preg_replace('/\s+/','',str_replace(',', '.', $row_data[3])); 

datatype of price field in table varchar, when used int or decimal skips digits after space (2 070 becomes 2, 12 015 => 12). have remove space in integration stage, because when operations price field php skips digits after space , have errors in further manipulations.

what problem guys?

upd solution @jeroen works great, changed field of price decimal (10,2) , put line parsing string price:

$model->price = filter_var(str_replace(',', '.', $row_data[3]), filter_sanitize_number_float, filter_flag_allow_fraction);  

i not use regex filter that:

// replace if necessary $number = str_replace(',', '.', $row_data[3]);  // filter unwanted characters $model->price = filter_var($number, filter_sanitize_number_float, filter_flag_allow_fraction); 

and should make sure update prices in database can switch numerical value in database itself.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -