Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts

    advanced explode

    Hi,

    I'm currently using explode() to split a string with different values seperated by comma's. This works perfect, untill there appears to be a comma in the value itself (e.g. name, street, city, 'message, with a comma in it', date).

    Instead of 6 pieces, this string should result in only 5:

    $array[] = "name";
    $array[] = "street";
    $array[] = "city";
    $array[] = "message, with a comma in it";
    $array[] = "date'";

    Now my question is whether there is a standard function that does what I want?

    I hope I'm clear! Thanx in advance, Michiel

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    IF your string is exactly how you're showing it; i.e. all messages with a comma are contained in single quotes, you might want to look into using preg_match_all to seperate the values.

    Not sure if you can do this with explode.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    thanx for your reply. You're right that all messages containing comma's will be encapsulated in single quotes. However, other values without comma's may have quotes around them as well.

    I guess you're right about the regular expressions. I'm not a pro, with them, so maybe you have some tips for me?

    Thanx! Michiel

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    a much easier sollution is to use a different seperator. Like | or so. (which is what i usualy do)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or explode(", ",$string);

    sorry that wont work as theres a space after , in message

    best use a different seperator
    Last edited by sidney; 04-13-2004 at 02:56 PM.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your replies,

    @raf: using a different seperator is an option, but I still prefer to use a comma. On top of that using '|' is not really solving the problem, but making it occur less often ... Would you happen to know how to achieve the result with rgular expressions?

    @sidney: yes, I had thought of that myself as well, but that won't work ...

    Michiel

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Michiel
    @raf: using a different seperator is an option, but I still prefer to use a comma. On top of that using '|' is not really solving the problem, but making it occur less often ... Would you happen to know how to achieve the result with rgular expressions?
    well, i never came across a stringvalue with | in it but if you like you could use a seperator like §|§ or whatever. If you prefere to use a comma, then you're on your own. I don't see why preg_match_all would be better (it's still just cutting it up based on a seperator and preg_match_all will have the same problems as explode if the future elements contain comma's). It would probably be better to use preg_split anyway (if you must go the regex way)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts

    surreal answer

    ok this is a potty way of doing it but humour me , I have been playing with the streams functions and working on a similar issue , this is my first proper shot at streams so its basic but does appear to work to a point

    an alternative is to save the string to file and read using fgetscsv()

    regular expressions are probably more straightforward though I would be interested to know which is actually faster... if anyone can supply good efficient regex example I will test difference

    PHP Code:
    <?php
    class csvstream{
       var 
    $position
       var 
    $varname
       function 
    stream_open($path$mode$options, &$opened_path){ 
           
    $url parse_url($path); 
            
    $this->varname $url['host'] ;
           
    $this->position 0
           return 
    true;
       }
      function 
    stream_read($count){ 
           
    $ret substr($GLOBALS[$this->varname], $this->position$count); 
           
    $this->position += strlen($ret); 
           return 
    $ret
       }
      function 
    stream_eof(){ 
           return 
    $this->position >= strlen($GLOBALS[$this->varname]); 
       } 
       function 
    stream_tell(){ 
           return 
    $this->position
       } 
    }

    /*register the class above*/
       
    stream_wrapper_register("csvstr""csvstream") ;
    /*string to parse*/
       
    $str="name, street, city, 'message, with a comma in it', date";

    /*open and read like a normal file handle*/
       
    $fp fopen("csvstr://str""r+"); 
       
    print_r(fgetcsv($fp,100,",","'"));

    ?>
    not yet worked out how to use filesize() in this scenario nor fclose() (if even required)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Quote Originally Posted by firepages
    regular expressions are probably more straightforward though I would be interested to know which is actually faster... if anyone can supply good efficient regex example I will test difference
    <cough>...Mordred...</cough>

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi guys,

    a friend of mine noticed that the format I was using (name, city, street, 'message, with a comma', date) was exactly the comma seperated values format used in M$ Excel.

    With that information I came accross the following script at php.net:

    PHP Code:
                                                                                     
    //Explode CSV (MS Excel) string                                                  
                                                                                     
    function csv_explode($str$delim ','$qual "'")                            
    {                                                                                
       
    $len strlen($str);                                                          
       
    $inside false;                                                              
       
    $word '';                                                                   
       for (
    $i 0$i $len; ++$i) {                                               
           if (
    $str[$i]==$delim && !$inside) {                                       
               
    $out[] = $word;                                                       
               
    $word '';                                                           
           } else if (
    $inside && $str[$i]==$qual && ($i<$len && $str[$i+1]==$qual)) {
               
    $word .= $qual;                                                       
               ++
    $i;                                                                 
           } else if (
    $str[$i] == $qual) {                                           
               
    $inside = !$inside;                                                   
           } else {                                                                  
               
    $word .= $str[$i];                                                    
           }                                                                         
       }                                                                             
       
    $out[] = $word;                                                               
       return 
    $out;                                                                  
    /*-------csv_explode()------------*/                                           
                                                                                     
    //Test...                                                                        
    $csv_str "name, street, 'message, with a comma'";                          
    print_rcsv_explode($csv_str) ); 
    It seems to be working just as I want. I don't understand anything of the code on the other hand ... can someone explain what is happening in the script? I really like to know and I guess that I can learn a lot of it!

    Cheers, Michiel


  •  

    Posting Permissions

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