-4

I have a sample array like this:

[
  "BTCUSD",
  "DASHBTC",
  "DOGEUSD",
  "LTCBTC",
  "LTCUSD",
  "SCBTC",
  "STEEMBTC",
  "WAVESBTC",
  "SNGLSBTC",
  "1STBTC",
  "DASHUSD",
  "BQXETH",
  "PTOYETH",
  "XAURETH",
  "BTCUSDT"
]

How to get just one currency for every string? I can't use explode because there isn't a single/static delimiter. I can't use substr() or strpos() because the substrings vary.

So how to split it?

Update

This is my expected output

[
   "BTC",
   "DASH",
   "DOGE",
   "LTC",
   "LTC",
   "SC",
   "STEEM",
   "WAVES",
   "SNGLS",
   "1ST",
   "DASH",
   "BQX",
   "PTOY",
   "XAUR",
   "BTC"
]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
skadevz
  • 57
  • 1
  • 1
  • 7

3 Answers3

1

You need to have an array to your expected currencies and then filter against it.

Here is a quick and dirty solution: https://3v4l.org/6mMbN or with recursive function call: https://3v4l.org/mhjBD

<?php

$currencies = [
    'USD',
    'BTC',
    'DASH',
    'LTC',
    'SC',
    'STEEM',
    'WAVES',
    'SNGLS',
    '1ST',
    'BQX',
    'ETH',
    'PTOY',
    'XAUR',
    ];

$input = [
  "BTCUSD",
  "DASHBTC",
  "DOGEUSD",
  "LTCBTC",
  "LTCUSD",
  "SCBTC",
  "STEEMBTC",
  "WAVESBTC",
  "SNGLSBTC",
  "1STBTC",
  "DASHUSD",
  "BQXETH",
  "PTOYETH",
  "XAURETH",
  "BTCUSDT",
];

$output=[];
foreach($input as $doubleCurrency){
    foreach($currencies as $currency){
        $pattern = '/^'.$currency. '/';
        preg_match($pattern, $doubleCurrency, $matches);
        if(array_key_exists(0, $matches)){
            $output[]=$matches[0];
        }
    }
}

var_dump($output);
Edwin
  • 2,146
  • 20
  • 26
1

You are always trimming the right side of the string, just use preg_replace() with an end of string anchor. No lookup array is necessary. It doesn't get much easier that this:

Code: (Demo)

$input=[
  "BTCUSD",
  "DASHBTC",
  "DOGEUSD",
  "LTCBTC",
  "LTCUSD",
  "SCBTC",
  "STEEMBTC",
  "WAVESBTC",
  "SNGLSBTC",
  "1STBTC",
  "DASHUSD",
  "BQXETH",
  "PTOYETH",
  "XAURETH",
  "BTCUSDT"
];

var_export(preg_replace('/USDT$|USD$|ETH$|BTC$/','',$input));

Output:

array (
  0 => 'BTC',
  1 => 'DASH',
  2 => 'DOGE',
  3 => 'LTC',
  4 => 'LTC',
  5 => 'SC',
  6 => 'STEEM',
  7 => 'WAVES',
  8 => 'SNGLS',
  9 => '1ST',
  10 => 'DASH',
  11 => 'BQX',
  12 => 'PTOY',
  13 => 'XAUR',
  14 => 'BTC',
)

(The pattern can be condensed if you don't mind the syntax. /USDT?$|ETH$|BTC$/ is a little faster.)

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

Here is an answer that is similar to Edwin's but is probably faster.
I use str_replace to create a space in-between the currencies then I loop the new array and remove anything after the first space.

Example:
'STEEMBTC' becomes STEEM BTC '.
Then substr it to: 'STEEM'.

Probably quicker code as it loops less and does not use regex.

$currencies = [
  'USD',
  'BTC',
  'DASH',
  'LTC',
  'SC',
  'STEEM',
  'WAVES',
  'SNGLS',
  '1ST',
  'BQX',
  'ETH',
  'PTOY',
  'XAUR',
  'DOGE'
];

$repl = [
  'USD ',
  'BTC ',
  'DASH ',
  'LTC ',
  'SC ',
  'STEEM ',
  'WAVES ',
  'SNGLS ',
  '1ST ',
  'BQX ',
  'ETH ',
  'PTOY ',
  'XAUR ',
  'DOGE '
];

$input = [
  "BTCUSD",
  "DASHBTC",
  "DOGEUSD",
  "LTCBTC",
  "LTCUSD",
  "SCBTC",
  "STEEMBTC",
  "WAVESBTC",
  "SNGLSBTC",
  "1STBTC",
  "DASHUSD",
  "BQXETH",
  "PTOYETH",
  "XAURETH",
  "BTCUSDT",
];

$new = str_replace($currencies, $repl, $input);
Foreach($new as &$item){
    $item = substr($item, 0, strpos($item, " "));
}

Var_dump($new);

https://3v4l.org/QDMjS

Andreas
  • 23,610
  • 6
  • 30
  • 62