0

I have this string: $delims = ',^.;:\t' (which is used in a preg_replace() call).
I need two arrays .. one with those delimiters as the index ($delimCount), and another with those delimiters as the values ($delimiters).

The former will get values assigned from within a loop on the latter .. as such:

foreach ($delimiters as $delim) { 
    if( strpos($char, $delim) !== false) { // if the char is the delim ...
        $delimCount[$delim]++; // ... increment
    }
}

The way I have it now is messy, and I'd like to simply break that string into the two arrays ... but I'm getting tripped up on the \t delimiter (because it's the only one with two characters).
How can/should I handle that?

5
  • Why didn't you write $delims = ",^.;:\t"; instead? Commented Jul 11, 2012 at 13:46
  • 1
    Use a double-quoted string instead. \t is one character then. Commented Jul 11, 2012 at 13:46
  • And, by the way, if you need to split some string into smaller ones with different tokens, you might be interested in strtok function. Commented Jul 11, 2012 at 13:47
  • Yes - PHP doesn't parse special characters in strings wrapped in single quotes, only double quotes Commented Jul 11, 2012 at 13:48
  • Thanks, but if I dbl-qt the string, then my index in the $delimiters array becomes an interpreted tab rather than '\t' which conflicts with a later need/usage ... maybe it's time for me to look at whether that later usage REALLY needs it as a string or not ... maybe I can just change that code and leave this problem with the simple/elegant solution proposed. Thanks-a-bunch. Let me know if you have any other thoughts. Commented Jul 11, 2012 at 13:57

3 Answers 3

1

How I would handle your \t delimiter

$delims = ',^.;:\t';

$i = 0;

while($i < strlen($delims)) {
    if($delims[$i] == chr(92)) {
        $delimiters[] = $delims[$i] . $delims[$i+1];
        $i = $i + 2;
    }
    else {
        $delimiters[] = $delims[$i];
        $i++;
    }
}

Output of $delimiters

Array
(
    [0] => ,
    [1] => ^
    [2] => .
    [3] => ;
    [4] => :
    [5] => \t
)

As far as an array with the delimiters as an index

foreach($delimiters as $key=>$val) {
    $delimCount[$val] = $val;
}

Output of $delimCount

Array
(
    [,] => ,
    [^] => ^
    [.] => .
    [;] => ;
    [:] => :
    [\t] => \t
)

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you .. while ALL of the answers were extremely helpful, your while loop is the one which specifically solved the question as I posted it .. so I ticked this one as the answer. Thanks again to everyone.
0

I need two arrays .. one with those delimiters as the index ($delimCount), and another with those delimiters as the values ($delimiters).

Well, where does $delimiters come from? Did you write it yourself? If so, I'd argue you'd be better off making $delimiters an array in any case, and using implode when you use it in preg_replace.

1 Comment

Thanks ... believe it or not, this was extremely helpful :)
0

If you have a problem with tabulator, then you can use ordinal number of character as the key inplace of character itself: $delim = "\t"; $delimCount[ ord($delim) ]++;

Handy complementary functions: ord() and chr().

1 Comment

Great suggestion ... ord() and chr() were new to me .. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.