/    Sign up×
Community /Pin to ProfileBookmark

Remove subset of data

Team,

I have a script that uploads a CSV file into a MySQL database. In this CSV file is a column of two sets of numbers. Ex: 48 / 54.

There are times when this set of numbers is 06 / 54 and interpreted by the spreadsheet creating the CSV as a date (06/01/54).

I need a way to determine of this field of data is set up like a date, and if so to remove the middle set (/01) or (01/) so my end result is 06 / 54

This line is for TxRxMBPS, line_of_data[4]

Code below.

[CODE]
while (($line_of_data = fgetcsv($file_handle, 1000, “,”)) !== FALSE) {
$line_import_query=”
INSERT into bridges (
MAC,
DeviceName,
SignalNoiseDBM,
Distance,
TxRxMbps,
CCQ,
ConnectionTime,
LastIP,
ComputerName,
ExpectedRig
) VALUES (
‘”.mysql_real_escape_string($line_of_data[0]).”‘,
‘”.mysql_real_escape_string($line_of_data[1]).”‘,
‘”.mysql_real_escape_string($line_of_data[2]).”‘,
‘”.mysql_real_escape_string($line_of_data[3]).”‘,
‘”.mysql_real_escape_string($line_of_data[4]).”‘,
‘”.mysql_real_escape_string($line_of_data[5]).”‘,
‘”.mysql_real_escape_string($line_of_data[6]).”‘,
‘”.mysql_real_escape_string($line_of_data[7]).”‘,
‘”.mysql_real_escape_string($compname).”‘,
‘”.mysql_real_escape_string($ExpectedRig).”‘
)
“;
mysql_query($line_import_query) or die(mysql_error());
}
[/CODE]

I appreciate your help

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 16.2014 — Sounds like a job for [url=http://php.net/preg_replace]preg_replace()[/url].
Copy linkTweet thisAlerts:
@TW79authorApr 16.2014 — Thank you, NogDog. I'm reading up on it now, but all the examples are using static text. I will need this to be a variable (the value of the column) and it will have to determine the number of characters, and execute only if there are exactly 8 characters.

I'll keep reading, and would appreciate if you could elaborate a bit on the best way to do this.
Copy linkTweet thisAlerts:
@NogDogApr 16.2014 — The quick-and-dirty solution would be to change this line (assuming it's a field that might have this issue)...
[code=php]
mysql_real_escape_string($line_of_data[5])
[/code]
...to something like...
[code=php]
mysql_real_escape_string(preg_replace('#b(d+)/d+/(d+)b#' "$1 / $2", $line_of_data[5]))
[/code]

Now your job is to figure out how the heck that regular expression works -- assuming I got it right. ?

PS: There probably is a SQL-base solution using regexes in the query itself.

PPS: Prepared statements in either PDO or MySQL[b]i[/b] could both clean the code up a bit and make it work at least somewhat more efficiently.
Copy linkTweet thisAlerts:
@TW79authorApr 16.2014 — Thank you again. I'll take a look at regexes. Never heard of that. BTW - I read the quote in your signature line each time I see your posts. It makes me push myself a little further each time I am working through a challenge.
Copy linkTweet thisAlerts:
@TW79authorApr 16.2014 — Using your example, I am getting the following error:

Parse error: syntax error, unexpected '"$1 / $2"' (T_CONSTANT_ENCAPSED_STRING) in C:xampp2htdocshtdocsSpeedtestexecutimpcsv.php on line 47

Line 47 reads as you suggested:

[CODE]
'".mysql_real_escape_string(preg_replace('#b(d+)/d+/(d+)b#' "$1 / $2", $line_of_data[5]))."',
[/CODE]


Any thoughts?
Copy linkTweet thisAlerts:
@NogDogApr 16.2014 — missing a comma between the first and second params (i.e. between the single-quoted string and the double-quoted string).
×

Success!

Help @TW79 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...