0

I have the following point: a xls file contains one column with codes. The codes have a prefix and a unique code like this:

- VIP-AX757
- VIP-QBHE6
- CODE-IUEF7
- CODE-QDGF3
- VIP-KJQFB
- ...

How can I get equal parts of strings or an array? perfect would be if I get an array like this:

- $result[VIP] = 3;
- $result[CODE] = 2;

An array with the found prefix and the sum of cells with that prefix. But the result is not so important at the moment.

I couldn't find a soloution how to get equal parts of two strings: how to compare this "VIP-AX757" and "VIP-QBHE6" and get a result that says: "VIP-" is the same prefix/part in this two strings?

Hope someone has an idea. thx!

1
  • So what is your code? Commented Jul 10, 2014 at 10:32

4 Answers 4

1

-drum roll- Time for a one-liner!

$result = array_count_values(array_map(function($v) {list($a) = explode("-",$v); return $a;},$input));

(Assumes $input is your array of codes)

If you are using PHP 5.4 or newer (you should be), then:

$result = array_count_values(array_map(function($v) {return explode("-",$v)[0];},$input));

Tested in PHP CLI:

screenshot of result

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

5 Comments

Thanks. Actually I am using a similar function - but: the "-" in "VIP-" was just an example. The Codes could also be: "VIP##AX757" and "VIP##QBHE6" than the prefix would be "VIP##".
You can adjust the function() { .. } part to do anything you want, whatever it needs to do to get the prefix.
exatly that what I am searching for: a way to find the prefix if I dont't know a delimiter.
Kinda tough when you've got nothing to go on. If you can define some kind of rule, such as "the prefix has letters followed by symbols", then you might have something to work with.
wanted a autofunction for that. only thing would be to compare subtr for subtr, maybe char for char to find equal parts - but that would melt the server away.. ;) the can have up to 200.000 entries. so: no fast php funtcion to do that? I found "similar_text()" but its only calculating the similar parts - if it would return "this is the similar part: xxxx". thx anyway!
0

If the prefix is always followed by a '-' then you can do something like this:-

foreach ($codes as $code) {
    $tmp = explode("-",$code);
    $result[$tmp[0]] += 1;
}
print_r($result);

Comments

0

Depends on the variability of the data, but something like:

preg_match_all('/^([^-]+)/m', $string, $matches);
$result = array_count_values($matches[1]);

print_r($result);

If you don't know that there is an - after the prefix but the prefix is always letters then:

preg_match_all('/^([A-Z]+)/im', $string, $matches);
$result = array_count_values($matches[1]);

Otherwise you'll have to define exactly what the prefix can contain if it's not the delimiter.

Comments

0

Since you stated via comment to Niet that you don't have a reliable delimiter, then we can only write a pattern that identifies your targeted substrings based on their location in each line.

I recommend preg_match_all() with no capture group, a start of the line anchor, and a multi-line pattern modifier (m).

I've written a preg_split() alternative, but the pattern is a little "clunkier" because of the way I'm handling the line returns.

Code: (Demo)

$string = 'VIP-AX757
VIP-QBHE6
CODE-IUEF7
CODE-QDGF3
VIP-KJQFB';

var_export(array_count_values(preg_match_all('~^[A-Z]+~m', $string, $out) ? $out[0] : []));
echo "\n\n";
var_export(array_count_values(preg_split('~[^A-Z][^\r\n]+\R?~', $string, -1, PREG_SPLIT_NO_EMPTY)));

Output:

array (
  'VIP' => 3,
  'CODE' => 2,
)

array (
  'VIP' => 3,
  'CODE' => 2,
)

Comments

Your Answer

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