PHP: Removing Substrings



  • Bit of a problem with some code that I'm working on: the script takes a 38000-something line CSV file, splits it into a two-dimensional array ($data[$line][$field]), and inserts it into a MySQL database, which works all well and good. However, I have to add code that takes the two-dimensional array and removes certain substrings from members of the array.

    I realize that the code isn't amazingly efficient, but here's what I've got for removing said substrings. (in the code below, $manuals is an array containing the 4 substrings "Manual 1000", "Manual 1001", "Manual 2001", and "Manual 2002" which are supposed to be removed):

    <font face="Courier">for($clean=0; $clean < count(file($gagedata)); $clean++)
    {
    str_ireplace($manuals, "", $data[$clean]);

    for($strip=0; $strip < 22; $strip++)
    {
    str_ireplace($manuals, " ", $data[$clean][$strip]);

    	//GAGE2ARRAY() adds two blank spaces to the end of every entry. Remove these.<br />
    	$end = strlen($data[$clean][$strip]);<br />
    	$noquote = $end - 2; <br />
    	$data[$clean][$strip] = mysql_real_escape_string(substr($data[$clean][$strip], 1, $noquote));<br />
    }<br />
    	//Used for debug purposes.<br />
    //echo($clean . &quot;--- &quot; . $data[$clean][0] . &quot;<br />
    

    ");

    }</font>

    Now here's the problem: it removes the blank spaces at the end of every line, but the str_ireplace() doesn't work and the strings are inserted into the database with the Manual * still intact.

     Any ideas why it refuses to remove the Manual * even with str_ireplace()?


    Regards,
    Bradley Williams
    Northeast Metrology



  • Doesn't MySQL allow you to import CSV files?

    Anyway, whats wrong with parsing it out of the CSV file directly?

    sed -e "s/Model (1000|1001|2001|2002)//" < gagedata.csv > gagedata_clean.csv

    I'm assuming you want to replace all instances of "Model 1000", "Model 1001", etc... without space.

    Oh, and doesn't PHP have a string split which will let you split a CSV line into an array?

     



  • Unless I'm completely wrong,

    str_ireplace is a function that leaves the input parameters intact and returns a modified copy of the parameters, which your program discards.

    instead of   <font face="Courier">
    str_ireplace($manuals, "", $data[$clean]);</font>

    which does nothing, you have to write

    <font face="Courier">$data[$clean] = </font><font face="Courier">str_ireplace($manuals, "", $data[$clean]);</font>

    and instead of  <font face="Courier">str_ireplace($manuals, " ", $data[$clean][$strip]);</font>

    you need 

    <font face="Courier">$data[$clean][$strip] =</font><font face="Courier">
    str_ireplace($manuals, " ", $data[$clean][$strip]);</font><font face="Courier"></font>



  • Well, looks like I'm responsible for a WTF! You are right ammoQ.. I completely overlooked the fact that the returned value has to go somewhere. :-\ (trying to code and debug after 3 days without sleep = bad idea). Thank you for that.

     In reply to the person above, the problem with MySQL's native CSV import, as well as fgetcsv() in PHP is that some of the fields in the CSV contain double-quotation marks, which the CSV uses as enclosure for fields, and I can't change the enclosure (the CSV is getting created from a Paradox DB which an in-house program uses, and the conversion tool only allows using " as the field enclosure). So, when the native functions try to parse it, it creates all sorts of messes where data isn't where it should be. So, I had to write my own code to properly split it, and insert it into a database.
     

    Regards,
    Bradley Williams
    Northeast Metrology


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.