SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: find and replace function

  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy find and replace function

    Hi I am looking to do a sort of find and replace using php.
    I have a .csv file which I need to change some things in before I upload it to a database.
    One of the lines of data in the file is as follows:

    1832208,501455860,Vauxhall Astra 1.6 Active 5-Door Hatchback,4994,Ultra Blue,charcoal piazza cloth,28688,2004,MAR,SK04PHY,Manual,[Anti-lock brakes,Driver seat height adjustment,Driver and passenger air bags,Air-conditioning,Alloy wheels,Remote central Door locking,Manual Door mirrors,Front electric windows,Front/rear head restraints,Headlamp adjustment,5 speed manual gearbox,Power-assisted steering,Rear wash wipe,Rev counter,Split rear seat,Rake/reach adjustable steering column/wheel,Immobiliser,Compact disc player,Audio steering wheel mounted controls]

    The part coloured in red above is where I have the problem. I need to
    1) replace all of the , in this section with # and then
    2) remove the square brackets

    I can only manage to get one of the thing mentioned above to change I can't get it to change both things and in that order otherwise it will mess up the whole file.

    Does any one have any suggestions?


    Thanks

  2. #2
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    The Big Apple
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read up on the function str_replace()

    http://us.php.net/manual/en/function.str-replace.php
    SK

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    find and replace

    Thanks for that link. I've had a look and there's alot of useful stuff there but I still can't see anything to do what I'm looking for.
    Sorry if I'm neing a bit thick with this, but any help would be greatly appreciated.

  4. #4
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    The Big Apple
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you put the part you need to fix in a variable like so:

    PHP Code:
    $data 'word,word,word,word,[word],[word]'//whatever 
    And then use the str_replace function to do what you need:

    PHP Code:
    $new_data str_replace(',''#'$data);//replace commas
    $new_data str_replace('['''$new_data); //replace left bracket
    $new_data str_replace(']'''$new_data); //replace right bracket 
    Something like that, you can actually get rid of everything in on shot using REGEX but the above should be enough to get the job done.
    SK

  5. #5
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,618
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    list($data1,$data2) = explode('[',$data,2); //assuming $data contains your string
    $data2 str_replace(array(',',']'),array('#',''),$data2);
    $data $data1.$data2
    As SharifTK mentions, some fancy REGEX can probably do this all in one shot, but it's quite possible that following either his or my suggestions will in fact be faster than whatever tricky REGEX is needed for this.
    PHP questions? RTFM
    MySQL questions? RTFM

  6. #6
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    The Big Apple
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And also to address another issue in the thread starters post, you will need to open the .csv file - and figure out how to extract the data and put it into a usable variable.

    www.php.net has all the documentation you need to do this.
    SK

  7. #7
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help on this, its been cracking me up!
    The scripts that you've put up have been good but they are not doing quite what I need them to. Using the format of the script that SharifTK put up it replaces the square brackets with nothing, which is ideal and it also replaces the commas with # but my problem is that I need the commas outside of the square brackets to remain as commas and only the ones that appear inside the brackets to change.

    Any suggestions as to how I manage to do that?

  8. #8
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,618
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script I posted will do that - it doesn't touch anything outside of the square brackets at all.
    PHP questions? RTFM
    MySQL questions? RTFM

  9. #9
    SitePoint Guru SharifTK's Avatar
    Join Date
    Jan 2004
    Location
    The Big Apple
    Posts
    621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kromey maybe you should elaborate for sketchgal on how exactly it works.

    And sketchgal, one way to define somebody's code if you don't understand it, look up what each function in their code is used to do.
    SK

  10. #10
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah I'd made a mistake when I'd tried using your code Kromey. Your right it does change the commas inside the square brackets and leave the ones before it as they are but it changes the square brackets to # aswel. I'm having a look around like SharifTK advised but do you know how can I stop it doing this?

  11. #11
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,618
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, it should not be changing the square brackets... the first square bracket '[' disappears entirely from the explode function; the other ']' should be replaced with the empty string '' in the str_replace function. I've tried this code out myself on the example string you provided in your first post, and it works exactly as expected. Can you post the relevant code section from your script? I suspect there was a minor error when adapting my snippet to your script.
    PHP questions? RTFM
    MySQL questions? RTFM

  12. #12
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Kromey, pasted below is the code that I am using:


    <?php
    include('../info.php');
    $conn = mysql_connect("$DBHost","$DBUser","$DBPass") or die("Db Connection Error" . mysql_error());
    mysql_select_db($DBName, $conn) or die ('Can\'t use $DBName : ' . mysql_error());

    mysql_select_db('test')
    or die('Cannot connect to database');

    $myFile = "cars.csv";
    $fh = fopen($myFile, 'r');
    $data = fread($fh, filesize($myFile));
    fclose($fh);

    list($data1,$data2) = explode('[',$data,2); //assuming $data contains your string
    $data2 = str_replace(array(',',']'),array('#',''),$data2);
    $data = $data1.$data2;

    echo $data;
    ?>


    The csv file that I'm reading the data from has several lines in it like the one that I pasted earlier, here's a sample of it again:

    PFC Dealer Id,PFC Id,Vehicle Description,Price,Colour,Interior Colour & Material,Mileage,Registration Year,Registration Month,Registration,Transmission, [,Equipment,...],,,,,,,,,,,,,,,,

    1832208,501455860,Vauxhall Astra 1.6 Active 5-Door Hatchback,4994,Ultra Blue,charcoal piazza cloth,28688,2004,MAR,SK04PHY,Manual,[Anti-lock brakes,Driver seat height adjustment,Driver and passenger air bags,Air-conditioning,Alloy wheels,Remote central Door locking,Manual Door mirrors,Front electric windows,Front/rear head restraints,Headlamp adjustment,5 speed manual gearbox,Power-assisted steering,Rear wash wipe,Rev counter,Split rear seat,Rake/reach adjustable steering column/wheel,Immobiliser,Compact disc player,Audio steering wheel mounted controls]

    1832208,501460398,Ford Ka 1.3 Style 3-Door Hatchback,3199,Deep Navy,blue cascade cloth,39324,2003,MAR,SG03WYH,Manual,[Driver seat height adjustment,Driver air bag,Manual Door mirrors,Front/rear head restraints,5 speed manual gearbox,Power-assisted steering,Rear wash wipe,Immobiliser,Compact disc player],,,,,,,,,,

    1832208,501472525,Ford Fiesta 1.4 Zetec 3-Door Hatchback,4495,Colorado Red,black kompass,26898,2003,May,SB03LXH,Manual,[Driver seat height adjustment,Driver and passenger air bags,15in alloy wheels,Manual Door mirrors,Front electric windows,Front/rear head restraints,Power-assisted steering,radio/CD,Rear wash wipe,Rev counter,Split rear seat,Rake adjustable steering column/wheel,Immobiliser,Front fog lights],,,,,



    Any ideas as to where I'm going wrong with this? Thanks again for the help with this.

  13. #13
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,618
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see the problem! You are reading your entire file into $data all at once. My code snippet only works if you have only a single line in $data.

    In this case, you need to either come up with some super-fancy REGEX that can do it all at once, our else you need to read in line-by-line if you want to use my code:

    PHP Code:
    $myFile "cars.csv";
    $fh fopen($myFile'r');

    while(!
    feof($fh))
    {
        
    $data fgets($fh);
        list(
    $data1,$data2) = explode('[',$data,2);
        
    $data2 str_replace(array(',',']'),array('#',''),$data2);
        
    $data $data1.$data2

        echo 
    $data;

    PHP questions? RTFM
    MySQL questions? RTFM

  14. #14
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Kromey

    Thats great thanks alot for that. Your code is great!
    I'll get that set up now. Appreciate the help once again!

  15. #15
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,618
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not a problem - that's why we're all here.
    PHP questions? RTFM
    MySQL questions? RTFM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •