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

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.