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?
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.
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?
@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 ...
@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)
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)
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)
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
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:
//Test...
$csv_str = "name, street, 'message, with a comma'";
print_r( csv_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!