Jump to content

stripping special characters from a database field for csv


mcfcben

Recommended Posts

Hi guys,

 

I'm currently working on a piece of code, where I'm aiming to strip special characters such as '.' and '-', leaving just numbers and letters format, this data needs to be exported to a csv file and held there.

 

I've researched preg_replace and decided to use it, and found that the code I created did work when random text/characters was used in the $input array and an echo on the page was tested

 

$input .="c.products_model";
$output = preg_replace("/[^a-zA-Z0-9\s]/","",$input); 

 

(in the above code, c.products_model is relating to the database field I need to call and modify, then be shown in the csv file with special characters stripped out)

 

the SQL query I'm using to export the data to csv is structured like this: - i dont think  this is where the issue lies but thought i'd include it just in case

 

$sql_query = "SELECT concat( '" . $productURL . "' ,b.products_id) AS product_url, a.manufacturers_name, c.products_model, $output AS products_model_2 from $table WHERE a.manufacturers_id = c.manufacturers_id AND b.products_id = c.products_id AND $wherecond ORDER BY $ordersort";

 

Where am I going wrong here? I've tried a few workarounds and I'm postive the issue is regarding $input as the actual preg_replace code ive called does work when i change the input variable

 

 

been tweaking this more, and now ive got it working with csv a bit better

 

my main problem is that preg_replace refuses to recognise that im trying to pass a table field to be edited, not a string (it strips c.products_model of the . and _ which is great as it shows the preg_replace actually works... but i want c.products_model to be called and the data within this field to be stripped... :/)

 

its probably a really basic fix, but im stumped!

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.