Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-13-2004, 12:54 PM   PM User | #1
Michiel
Regular Coder

 
Join Date: Jul 2002
Location: The Netherlands
Posts: 252
Thanks: 0
Thanked 0 Times in 0 Posts
Michiel is an unknown quantity at this point
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
Michiel is offline   Reply With Quote
Old 04-13-2004, 01:05 PM   PM User | #2
bcarl314
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
bcarl314 will become famous soon enough
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.
bcarl314 is offline   Reply With Quote
Old 04-13-2004, 01:10 PM   PM User | #3
Michiel
Regular Coder

 
Join Date: Jul 2002
Location: The Netherlands
Posts: 252
Thanks: 0
Thanked 0 Times in 0 Posts
Michiel is an unknown quantity at this point
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
Michiel is offline   Reply With Quote
Old 04-13-2004, 01:23 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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
raf is offline   Reply With Quote
Old 04-13-2004, 02:51 PM   PM User | #5
sidney
Regular Coder

 
Join Date: Mar 2004
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
sidney is an unknown quantity at this point
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..
sidney is offline   Reply With Quote
Old 04-13-2004, 03:04 PM   PM User | #6
Michiel
Regular Coder

 
Join Date: Jul 2002
Location: The Netherlands
Posts: 252
Thanks: 0
Thanked 0 Times in 0 Posts
Michiel is an unknown quantity at this point
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
Michiel is offline   Reply With Quote
Old 04-13-2004, 03:26 PM   PM User | #7
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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
raf is offline   Reply With Quote
Old 04-13-2004, 03:44 PM   PM User | #8
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 04-13-2004, 04:29 PM   PM User | #9
bcarl314
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
bcarl314 will become famous soon enough
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>
bcarl314 is offline   Reply With Quote
Old 04-13-2004, 04:43 PM   PM User | #10
Michiel
Regular Coder

 
Join Date: Jul 2002
Location: The Netherlands
Posts: 252
Thanks: 0
Thanked 0 Times in 0 Posts
Michiel is an unknown quantity at this point
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
Michiel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:13 PM.


Advertisement
Log in to turn off these ads.