Page 1 of 1
gettoken and a list of string-tokens
Posted: 23 Sep 2016 10:19
by autocart
Hi all,
Current example is a ";" separated csv file created by a third party program.
all values are strings (enclosed in double quotes).
Inside it looks like that:
"abc";"def";"ghi"
"xyz";"uvw";"rst"
Issue: I thought of using gettoken on the file content but of course any value can contain the ";" character as well (and double quotes which are escaped by a second double quote: "").
In effect I would need for gettoken to recognize the strings and only take into account the separators outside of the strings when tokenizing a line of tokens.
E.g. gettoken('"abc"";";"def"', 1, ";") should return '"abc"";"'
Is this already possible in a somewhat easy way? I am also fine with methods other than gettoken as long as the result is correct.
any ideas?
Many thx.
Re: gettoken and a list of string-tokens
Posted: 23 Sep 2016 10:40
by highend
Write a function (so called state machine), that takes a $string, $index and $separator as arguments
and use the new, improved foreach loop ($separator = '') to parse each char of $string (or the good old plain while loop...).
The state switches depending on how many occurrences of $separator (") are found. The state starts with false (because the first char
could already be a ";"). It switches to true if an uneven number of separators is detected (use modulo (%) to determine if your count variable is dividable by 2). Two additional variables must be set to determine the length of the token to be returned.
If state is true the ";" is not a correct delimiter (uneven numbers of '"') in front of it, if false -> correct delimiter, store the token by a substr($cntFirstPos, $cntLastPos) depending on if $index matches the $index from the function call
Re: gettoken and a list of string-tokens
Posted: 23 Sep 2016 11:03
by autocart
thx for the recipe, highend.
I was hoping that I would have overlooked some already build-in method to do it or some already existing code on the forum here.
But with ur input I have to do less thinking when writing it on my own. Thx again.

Re: gettoken and a list of string-tokens
Posted: 23 Sep 2016 11:11
by PeterH
Note: in the following " is always meant to be part of the mentioned string, not as a delimiter. So specify the string ";" as
$delimit = '";"';
Dependent on the complexity of your data (may there be ";" inside the strings?) you might just treat the 3 characters ";" as separator.
In this case it would be neccessary to strip the very first and very last " of the complete data before starting to work on it.
Re: gettoken and a list of string-tokens
Posted: 23 Sep 2016 11:31
by autocart
Thx, PeterH, for ur concern.
I think I got it, though.
However, since its still more work than a one-liner, I postponed it until whenever and am doing a manual work-around for now.
Re: gettoken and a list of string-tokens
Posted: 26 Sep 2016 07:14
by autocart
BTW, PeterH,
in case u are referring to using gettoken like that (having the first and last " stripped):
gettoken('abc"";";"def', 1, '";"');
then it would, in this example return 'abc"' and not what it should return: 'abc"";'
Therefore, it seems that a user defined function would be unavoidable.
Thx, nevertheless.