...

View Full Version : advanced explode



Michiel
04-13-2004, 12:54 PM
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

bcarl314
04-13-2004, 01:05 PM
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.

Michiel
04-13-2004, 01:10 PM
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

raf
04-13-2004, 01:23 PM
a much easier sollution is to use a different seperator. Like | or so. (which is what i usualy do)

sidney
04-13-2004, 02:51 PM
or explode(", ",$string);

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

best use a different seperator

Michiel
04-13-2004, 03:04 PM
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

raf
04-13-2004, 03:26 PM
@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)

firepages
04-13-2004, 03:44 PM
;) 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
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)

bcarl314
04-13-2004, 04:29 PM
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>

Michiel
04-13-2004, 04:43 PM
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:




//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_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!

Cheers, Michiel



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum